Forum Moderators: coopster

Message Too Old, No Replies

Variable Checking?

Check if a variable is there..

         

NogginAnimations

11:19 pm on Jan 24, 2008 (gmt 0)

10+ Year Member



Okay, so I have my list of users. It's in the following format;

"User1,User2,User3,User4,Use..."

and I really need a code to check if a certain Username is in the list.
I've already tried using this;


$result2 = mysql_query("SELECT * FROM stuff WHERE id='$id' ") or die(mysql_error());
while($r2 = mysql_fetch_array($result2))
{
$username = "User1";
$users = $r2['users'];
if (str_replace($username,"#",$users)) {
$exists = "yes"; } else { $exists = "no";
}

And it doesn't work, but I'm not sure where to turn now..

Help on this would be greatly appreciated

eelixduppy

2:38 am on Jan 25, 2008 (gmt 0)



Try something like the following (This is assuming the query only returns one row to begin with):

$result2 = mysql_query("SELECT * FROM stuff WHERE id='$id' ") or die(mysql_error());
$r2 = mysql_fetch_array($result2);
$username = "User1";
$users = [url=http://www.php.net/explode]explode[/url](',',$r2['users']);
if ([url=http://www.php.net/array-key-exists]array_key_exists[/url]($username, $users))
{
$exists = "yes";
} else {
$exists = "no";
}