Execute code if mySQL query produces no results

My webpage runs the following code 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 the notifications 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. Am I using (mysql_num_rows($result)==0) correctly in this instance?

$insertNotification = "INSERT INTO notifications (date, priority, type, sender, recipient, subject, message) VALUES (

'$longDate',
'1',
'$type',
'$sender',
'$guideEmail',
'$subject',
'$message')";


$alreadyLogged = "SELECT date FROM referrals WHERE rID = '$rID' and arrived is null";

$result = mysql_query($alreadyLogged) or die("<b>A fatal MySQL error occurred</b>.\
<br />Query: " . $sql . "<br />\
Error: (" . mysql_errno() . ") " . mysql_error());


if (mysql_num_rows($result)==0) {

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

If the insert is always executed, apparently the select never returns any results.

Do an echo of $alreadyLogged and see if the query is what you expect.
Then copy and paste it in phpMyAdmin and see if it gives you the expected result.

Am I using (mysql_num_rows($result)==0) correctly in this instance?

Not exactly sure in your particular case, but to shed some light on the issue I quoted above: this is typically where you would use ===0 because a condition dependent upon == 0 can be passed with the value ‘false’ – whereas you seemingly really want to be strict about it being an integer with the value 0.

Loose and strict comparisons in PHP