Getting NULLs confused

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Indochine
Forum Newbie
Posts: 5
Joined: Tue Jan 27, 2009 5:34 pm

Getting NULLs confused

Post 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.
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: Getting NULLs confused

Post by Skoalbasher »

try if($player = "")

or if(!$player)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Getting NULLs confused

Post 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.
Indochine
Forum Newbie
Posts: 5
Joined: Tue Jan 27, 2009 5:34 pm

Re: Getting NULLs confused

Post by Indochine »

Thanks for all the replies.

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