PHP checking for no record

PHP checking for no record

am 06.04.2007 18:07:47 von Bill F

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("
Result = $result, Mileage = $mileage, Date= $date");
printf("
Year = $year, Make = $make, Model = $model");
printf("
Category = $category");

if( !$result )
{
prinf("

create DMID");
}
else
{
print("

Found DMID");
//$DMID = mysql_fetch_object($result);
}

Re: PHP checking for no record

am 07.04.2007 04:05:30 von gordonb.areh0

>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("
Result = $result, Mileage = $mileage, Date= $date");
>printf("
Year = $year, Make = $make, Model = $model");
>printf("
Category = $category");
>
>if( !$result )
>{
> prinf("

create DMID");
>}
>else
>{
> print("

Found DMID");
> //$DMID = mysql_fetch_object($result);
>}
>