What am I doing wrong here? When the query returns 0 rows, it does not echo "Not Found". I even did an echo mysql_num_rows($result), and it displays 0.
$dbh=mysql_connect ("localhost", "testing", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("testingdb");
$sql = "SELECT * FROM testdb WHERE variable = '$variable' LIMIT 1";
$result = mysql_query($sql, $dbh) or die(mysql_error('reult error'));
while ($newArray = mysql_fetch_array($result)) {
$numrows = mysql_num_rows($result);
if(!mysql_num_rows($result) || mysql_num_rows($result) == 0)
{
echo "<b>Not Found</b>";
echo $numrows;
echo mysql_error($dbh);
}
This script outputs 1 if $result returns 1 row, and nothing if the $result has zero lines. I'm just trying to tell the user that the information requested was not found.
Alright. Now it's allowing me to display messages based on the presence of absence of a row. However, now it's not passing more than one variable. For example:
Are you trying to access the variables in the array like an associative array? If you are make sure you add the argument MYSQL_ASSOC to your mysql_fetch_array. ie:
I was getting the same error but for different reasons.
I had this at first:
$id_num_rows = mysql_num_rows($result) or die(mysql_error($dbc));
removing the " or die(mysql_error($dbc))" made it work correctly for me. When i had the "or die" phrase nothing would be returned. and basically the page stopped being processed. It wasn't so noticeable because I'm using jquery post to just retrieve some data without a page refresh. Maybe your page is just halting without giving an error.
I tried this code in my program and it worked. so maybe the error comes before this
$id_num_rows = mysql_num_rows($result) or die(mysql_error($dbc));
removing the " or die(mysql_error($dbc))" made it work correctly for me. When i had the "or die" phrase nothing would be returned. and basically the page stopped being processed. It wasn't so noticeable because I'm using jquery post to just retrieve some data without a page refresh. Maybe your page is just halting without giving an error.
Yeah, that's not a good way of doing it because if the number of rows returns 0 then it would see it as a false value and trigger the die() method.
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
« Reply to If statement not recognising when mysql_num_rows returns 0 results