3 Replies - 1226 Views - Last Post: 18 October 2008 - 05:53 PM Rate Topic: -----

#1 sunshinekiller   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 14-October 08

comparing array with mysql query

Post icon  Posted 18 October 2008 - 05:36 PM

I am basically just comparing each user with a number to see if they are in the database or not, if they are it wont load the new player sign up. Ive tried to play around to get it to work but its only comparing the first user id. How do i get it to do all the players in the column? Thanks for those that reply.

<?php
function isAPlayer($user)
{
$query = ("SELECT * FROM players");
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result))
{
	if($user == $row['userID'])
	{
	return true;
	}
	else
	{
	include("newPlayer.php");
	return false;
	}
}
}
?>



Is This A Good Question/Topic? 0
  • +

Replies To: comparing array with mysql query

#2 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1651
  • View blog
  • Posts: 8,523
  • Joined: 07-September 06

Re: comparing array with mysql query

Posted 18 October 2008 - 05:46 PM

It is only running through the loop once because you are telling it to return after the first time through the loop. Either true or false.

What you will want to do is something like so:
<?php
function isAPlayer($user)
{
$query = ("SELECT * FROM players");
$result = mysql_query($query) or die(mysql_error());
$bool = true;
while($row = mysql_fetch_array($result))
{
    if($user == $row['userID'])
    {
    return true;
    }
    else
    {
    $bool = false;
    }
}
if(!$bool){
    include("newPlayer.php");
    return false;
}
}
?>


That way it will only stop looping if it is going to return true, otherwise it loops through all the users and then returns false if $bool is false, that is easy enough, you don't even really need $bool or the if statement...

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,997
  • Joined: 08-August 08

Re: comparing array with mysql query

Posted 18 October 2008 - 05:48 PM

It only does the first record because you return no matter what the value.
if($user == $row['userID'])

You return with a value of true if the above is true, ELSE you return with a value of false. Since there is no way that you don't return, there is no way for the loop to continue.
Was This Post Helpful? 0
  • +
  • -

#4 sunshinekiller   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 14-October 08

Re: comparing array with mysql query

Posted 18 October 2008 - 05:53 PM

Thanks that worked, i also ended up trying in the sql syntax where userID = user) seemed to fix it as well i guess i was staring at it to long and couldnt make sense of it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1