Discussion:
PHP checking for no record
(too old to reply)
Bill F
2007-04-06 16:07:47 UTC
Permalink
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);
}
Gordon Burditt
2007-04-07 02:05:30 UTC
Permalink
Post by 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?
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) {
...
}
Post by Bill F
-----------
$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);
}
Loading...