Jump to content

multiple numbers in mysql column


ibda12u

Recommended Posts

I have a question. I am using a php login script that checks an access level in a table. This access level is just a number in a column called level in the table.

What I am wondering is, is there a way to input more than 1 number in the level column? IF so, is there something I can insert in between them, so that sql can tell that it's not just 1 number, but seperate numbers?

Link to comment
Share on other sites

You could probably do something like seperate them with comma's (make sure the table type is set to text) then use the explode() function in PHP to put them in an array then use the in_array() function with an if statement?

Not sure if that's the direction you're wanting to go or not.
Link to comment
Share on other sites

[!--quoteo(post=352258:date=Mar 6 2006, 04:01 PM:name=Gaia)--][div class=\'quotetop\']QUOTE(Gaia @ Mar 6 2006, 04:01 PM) [snapback]352258[/snapback][/div][div class=\'quotemain\'][!--quotec--]
You could probably do something like seperate them with comma's (make sure the table type is set to text) then use the explode() function in PHP to put them in an array then use the in_array() function with an if statement?

Not sure if that's the direction you're wanting to go or not.
[/quote]

I think that's the direction I'm looking for. I'm not super strong with explode and in_array yet (something to read up on. But I guess I'd like my code to at all the numbers in the array, and then allow access based on that.
Ex. suppose a page has an access lvl requiring 12.
My user has a 1,12,34 all in his level field.
So I could explode that data into an array. Then have my access script check to see if 12 is in the array?
Link to comment
Share on other sites

[!--quoteo(post=352271:date=Mar 6 2006, 05:13 PM:name=ibda12u)--][div class=\'quotetop\']QUOTE(ibda12u @ Mar 6 2006, 05:13 PM) [snapback]352271[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I think that's the direction I'm looking for. I'm not super strong with explode and in_array yet (something to read up on. But I guess I'd like my code to at all the numbers in the array, and then allow access based on that.
Ex. suppose a page has an access lvl requiring 12.
My user has a 1,12,34 all in his level field.
So I could explode that data into an array. Then have my access script check to see if 12 is in the array?
[/quote]

That would work.. Of course, you lose some SQL functionality this way as the database will have no idea that there are multiple values in that column.

The proper "SQL way" to do this is to create an additional table with the mappings.
Link to comment
Share on other sites

Exactly.

When you retrieve the data, it would be like 1,12,34...when you explode it, it will end up in an array.

[code]
$foo = array (
    [0] => 1,
    [1] => 12,
    [2] => 34
);
[/code]

Then with the in_array function you just wrap an if statement around it.

[code]
if ( in_array('1',$foo) ) {
    echo '1 was found in the array';
} else {
    echo '1 was not found in the array';
}
[/code]

Hope that helps some :).

[i]EDIT[/i]: And yea, as Xeno said, you could create a new MySQL table listing the permissions and do it from a MySQL perspective.
Link to comment
Share on other sites

[!--quoteo(post=352280:date=Mar 6 2006, 04:19 PM:name=Gaia)--][div class=\'quotetop\']QUOTE(Gaia @ Mar 6 2006, 04:19 PM) [snapback]352280[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Exactly.

When you retrieve the data, it would be like 1,12,34...when you explode it, it will end up in an array.

[code]
$foo = array (
    [0] => 1,
    [1] => 12,
    [2] => 34
);
[/code]

Then with the in_array function you just wrap an if statement around it.

[code]
if ( in_array('1',$foo) ) {
    echo '1 was found in the array';
} else {
    echo '1 was not found in the array';
}
[/code]

Hope that helps some :).

[i]EDIT[/i]: And yea, as Xeno said, you could create a new MySQL table listing the permissions and do it from a MySQL perspective.
[/quote]

Hmm.. I'm guesing that'd be a table with a access level, an id, and an foreign key back to the original table?
Link to comment
Share on other sites

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Hmm.. I'm guesing that'd be a table with a access level, an id, and an foreign key back to the original table?[/quote]

Correct. Another table is the way to go. Comma separated columns are bad. Bad!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.