PHP checking for no record

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));
$data=mysql_fetch_object($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");
}
else
{
print("<P>Found DMID");
//$DMID = mysql_fetch_object($result);
}
Bill F [ Fr, 06 April 2007 18:07 ] [ ID #1680417 ]

Re: PHP checking for no record

>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?

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) {
...
}

>
>-----------
>$result = mysql_query("select DMID from DateMileage where date = '$date' and mil
>eage = '$mileage'");
>printf("Rows = ".mysql_num_rows($result));
>$data=mysql_fetch_object($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");
>}
>else
>{
> print("<P>Found DMID");
> //$DMID = mysql_fetch_object($result);
>}
>
gordonb.areh0 [ Sa, 07 April 2007 04:05 ] [ ID #1681128 ]
Datenbanken » mailing.database.mysql » PHP checking for no record

Vorheriges Thema: MySQL table suddenly disappeared and a func table was created
Nächstes Thema: Is firewall preventing MySQL access?