PHP checking for no record

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I am trying to check for '$date' & '$mileage' but when I give them a value
that is not in the table $result = 2 & the 'if' statement fails to create
the new record, or just print "create DMID" in this case. $result seems to
always = 2. num_rows shows correct (0 or 1) & the DMID is printed if
$date & $mileage are in the table.

What am I doing wrong?

$result = mysql_query("select DMID from DateMileage where date = '$date' and mil
eage = '$mileage'");
printf("Rows = ".mysql_num_rows($result));
printf(" data= $data->DMID");

printf("<BR>Result = $result, Mileage = $mileage, Date= $date");
printf("<BR>Year = $year, Make = $make, Model = $model");
printf("<BR>Category = $category");

if( !$result )
    prinf("<P>create DMID");
    print("<P>Found DMID");
    //$DMID = mysql_fetch_object($result);

Re: PHP checking for no record

Quoted text here. Click to load it

if (!$result) tests whether the query has *FAILED*, which is not
the same thing as successfully returning an empty result.  $result
is a resource, not a numeric value (although it might look like one
when printed).  You get a failure for things like syntax errors in
the query, nonexistent tables, permission problems, etc.  I suggest
that the test you want is:

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

Quoted text here. Click to load it

Site Timeline