Is it possible to do this  
Author Message
sir_pancake





PostPosted: 2004-4-19 1:05:23 Top

php-general, Is it possible to do this Heya,

I'm have a problem with a php file and looking for information in
a mysql database.

Basically, in my database is the field "groups" which will
contain all the groups a user is registered too, for example, user
Bob is registered to groups: group1, group2, group3'.
(In the database that would shown in the column as
'group1-group2-group3')

Is it possible to then run a mysql query which looks at the string of
information in that column and finds a certain group, much like the
explode function in php which would break the string down and put
each value into an array.
I know I haventt worded this very well, so I'll type it up in the
way it should flow.

need to find users who are members of group 1
open sql database, examine groups column
break down the contents and look for 'group1'
return value.

if this can't be done? If not, is there any other way of doing it
without searching every field of database and examining the contents
in a php script?

Thanks a lot!



----------------------------------------
The post originated from PHP Freaks:
----------------------------------------
http://www.phpfreaks.com
http://www.phpfreaks.com/forums


 
Saint Amadeus





PostPosted: 2004-4-19 1:36:00 Top

php-general >> Is it possible to do this Einn wrote:
> Heya,
>
> I'm have a problem with a php file and looking for information in
> a mysql database.
>
> Basically, in my database is the field "groups" which will
> contain all the groups a user is registered too, for example, user
> Bob is registered to groups: group1, group2, group3'.
> (In the database that would shown in the column as
> 'group1-group2-group3')
>
> Is it possible to then run a mysql query which looks at the string of
> information in that column and finds a certain group, much like the
> explode function in php which would break the string down and put
> each value into an array.
> I know I haventt worded this very well, so I'll type it up in the
> way it should flow.
>
> need to find users who are members of group 1
> open sql database, examine groups column
> break down the contents and look for 'group1'
> return value.
>
> if this can't be done? If not, is there any other way of doing it
> without searching every field of database and examining the contents
> in a php script?
>
> Thanks a lot!
>
>
>
> ----------------------------------------
> The post originated from PHP Freaks:
> ----------------------------------------
> http://www.phpfreaks.com
> http://www.phpfreaks.com/forums
>
>
I sugest you a different approach:

three tables:

1) users (user_id, user_name, ...)

2) groups (group_id, group_name, ...)

3) users_groups(user_id, group_id)

then, when you looking for users who are members of group, you can use
this query:

select
user
from
users as u left outer join
(users_groups as u_g inner join
groups as g on u_g.group_id = g.group_id)
on u.user_id = u_g.user_id
where
g.group_name = 'group1'
;

It should be more efficient:), even if it looks a bit strange:)

Saint
 
J.O. Aho





PostPosted: 2004-4-19 1:38:00 Top

php-general >> Is it possible to do this Einn wrote:
> Heya,
>
> I'm have a problem with a php file and looking for information in
> a mysql database.
>
> Basically, in my database is the field "groups" which will
> contain all the groups a user is registered too, for example, user
> Bob is registered to groups: group1, group2, group3'.
> (In the database that would shown in the column as
> 'group1-group2-group3')

> need to find users who are members of group 1
> open sql database, examine groups column
> break down the contents and look for 'group1'
> return value.

SELECT * FROM users WHERE groups LIKE '%group1%';

% = any character combination/string



//Aho