Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array and mysql result problem 1

Status
Not open for further replies.

electricphp

Programmer
Feb 20, 2008
71
US
how come this works:
Code:
$existing_users=array('roshan','mike','jason');

$user_name="roshan";
//checking weather user exists or not in $existing_users array
if (in_array($user_name, $existing_users))
{
	//user name is not availble
	echo "no";
} 
else
{
	//user name is available
	echo "yes";
}

but this doesn't


Code:
$existing_users=array();
$query = "SELECT username FROM users";
$result = mysql_query($query);

while($row=mysql_fetch_row($result))
{
      $existing_users[] = $row;
}

mysql_free_result($result);

$user_name="roshan";
//checking weather user exists or not in $existing_users array
if (in_array($user_name, $existing_users))
{
	//user name is not availble
	echo "no";
} 
else
{
	//user name is available
	echo "yes";
}

even though roshan is inside the table named users
 
delete these lines
Code:
while($row=mysql_fetch_row($result))
{
      $existing_users[] = $row;
}
and substitute with
Code:
while($row=mysql_fetch_assoc($result)){
      $existing_users[] = $row['username'];
}

note that your code is case sensitive. you may wish to cast the case to upper/lower case before testing it.

e.g.
Code:
while($row=mysql_fetch_assoc($result)){
      $existing_users[] = strtoupper($row['username']);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top