Need PHP help testing NULL values returned from a mySQL query

My webpage runs the following excerpt every time someone goes to it. My query goes out to the referrals table and checks to see if a particular row has a date in the “arrived” column. If “arrived” is null, then my script attempts to do an insert into another table. The problem is that its doing the insert every time the page is loaded, even when that particular referral already has an “arrived” date. Can someone help me figure out what I’m doing wrong?

Thanks!

$alreadyLogged = “SELECT arrived FROM referrals WHERE rID = ‘$rID’”;
$result = mysql_query($alreadyLogged) or die("<b>A fatal MySQL error occurred</b>.
<br />Query: " . $sql . “<br />
Error: (” . mysql_errno() . ") " . mysql_error());

$row = mysql_fetch_row($result);

$arrived = $row[“arrived”];

if (is_null($arrived)) {

mysql_query($insertNotification) OR die("<b>A fatal MySQL error occurred</b>.
<br />Query: " . $insertNotification . “<br />
Error: (” . mysql_errno() . ") " . mysql_error());
}

You are getting the database NULL value and the PHP null value confused. One is never equal to the other. If you want to test for NULL in a field in a database you have to test it in the query because there is no way to test it in PHP.

So I should modify my query to the following?

SELECT arrived FROM referrals WHERE rID = ‘$rID’ and arrived is null

and then use this to determine if there was anything returned?

if ($row = mysql_fetch_row($result)) {

// execute code here that inserts a row into the notifications table
}

Does this look correct?

Thanks!