Page 1 of 1

Getting NULLs confused

Posted: Fri Feb 06, 2009 2:08 pm
by Indochine
Hello all.

Having a problem with my SQL->PHP code - bascially, I want the player name inserted in the first null player value (player1-4).
I'm using the code below, but $use is always null, not slot1-4 as I expected.

Code: Select all

 function playerJoinRoom($username, $roomid)
        {   
            // find the next NULL value in the $roomid and current players
            $query = mysql_query("SELECT `room_id`, `current_players`, `player1`, `player2`, `player3`, `player4` FROM `room_list` WHERE `room_id` = $roomid");
            $row = mysql_fetch_assoc($query);
            $cp_before = $row['current_players'];           
            
            if (is_null($row['player1'])):
                $use == 'slot1';
            elseif (is_null($row['player2'])):
                $use == 'slot2';
            elseif (is_null($row['player3'])):
                $use == 'slot3';
            elseif (is_null($row['player4'])):
                $use == 'slot4';
            else:
                $error == 'Clould not choose slot.';
            endif;
// etc.
}
 
I've also tried using:

Code: Select all

 if ($row['player1'] === null) {
$use == 'slot1';
} // etc.
 
And also tried setting $player1-4 outside of the if, like this:

Code: Select all

 $player1 = $row['player1'];
if ($player1 === null) {
$use == 'slot1';
} // etc.
 
However, none seem to work.

I was thinking that an SQL null is different to a PHP null, as some people on the PHP manual seem to be suggesting, but I don't know how change it... even if I changed the query to ignore null values I wouldn't know which ones were null, which is what I need to know.

Any suggestions appreciated! I'm not that great at PHP, so examples are very useful ;P

Thanks.

Re: Getting NULLs confused

Posted: Fri Feb 06, 2009 2:18 pm
by Skoalbasher
try if($player = "")

or if(!$player)

Re: Getting NULLs confused

Posted: Fri Feb 06, 2009 2:27 pm
by Benjamin
Do you have NULL enabled for those particular database fields? If not is_null() or $x === null won't work. You can test to see if it's an empty string by using the solution Skoalbasher provided.

I would recommend that you enable null values, in which case your initial code would work, or you could also use the empty() function.

Re: Getting NULLs confused

Posted: Fri Feb 06, 2009 2:33 pm
by Indochine
Thanks for all the replies.

However, I realised that $use == "slot1"; should be $use = "slot1"; and it fixed it.