Jump to content

mysql_num_rows() returns error on zero rows


otuatail

Recommended Posts

Hi I don't want errors on my page been reported on the web browser for all to see so I use this.

$total2 = mysql_num_rows($qCheckUser) or die ("Error1");

however if the query returns zero rows I get Error1 on the web page. This is not an error and

I don't want to taje out the or die()

 

Any sugestions. :confused:

Link to comment
Share on other sites

If you want no errors at all, you can turn display_errors off in php.ini (or in a htaccess file if you don't have access to php.ini). That would be a must in a production server.

 

If a query returns zero rows, that doesn't trigger an error, so post how your query looks and any variable used in it.

Link to comment
Share on other sites

Hi I have echo'd the sql and tested it in phpmy admin. Nothing wrong withe the query itself. Don't want to turn errors off.

 

function KeyValid($val,$type,$OptionalName)
{
   $sql = "SELECT Friendly,K_Type FROM SAYusersX WHERE UserKey = '$val' AND K_Type = $type";
 $query = mysql_query ($sql) or die ("E0105");
 $total = mysql_num_rows($query); // or die ("E1105");
   if($total == 0)
     $ret = 0;
   if($total > 1) // Should not return more than one row
     $ret = -1;
   if($total == 1) // OK!
     $ret = 1;

return $ret;
}

 

I get a blank web page with E1105. This only happens on zero returned rows.

 

Link to comment
Share on other sites

Try:

$query = mysql_query ($sql) or die (msql_error());

 

Check what error are you getting and post it here. I suspect the "K_Type" part is causing it, as it most probably is not numeric and you've not used quotes.

$sql = "SELECT Friendly, K_Type FROM SAYusersX WHERE UserKey = '$val' AND K_Type = '$type'";

Link to comment
Share on other sites

$total2 = mysql_num_rows($qCheckUser) or die ("Error1");

 

You need to understand what that statement is saying. "OR DIE" is NOT a construct of PHP. It is TWO operations:

 

OR -- is a logical operator

DIE() -- is a language construct.

 

The "OR DIE" sequence of constructs works, because PHP "short-circuits" conditionals. What that statement says is to evaluate mysql_num_rows($qCheckUser), if it is TRUE (i.e. NOT zero or anything else that PHP considers to be FALSE), then assign the value to $total2 -- OR if it is NOT TRUE (anything that PHP considers to be FALSE) evaluate the die() call. Since an OR will be TRUE if either expression is TRUE, the DIE() is not executed when the first expression is NOT FALSE. However, in this case, when mysql_num_rows() returns zero because there are zero rows, PHP considers it false and executes the die() function.

 

Instead of using "OR DIE" you should just do the assignment and follow-up with an IF test

 

$total2 = mysql_num_rows($qCheckUser);
if ($total2 < 1) {
  // WE DID NOT FIND ANY ROWS
  // DO WHATEVER YOU NEED TO DO WHEN THERE ARE NO ROWS
} else {
  // DO WHATEVER YOU DO WHEN YOU DO FIND ROWS
}

Link to comment
Share on other sites

mysql_query() doesn't return a boolean value; instead it returns a resource identifier that can't be evaluated to true/false if there isn't any error in the query. As I said in my previous post, an empty result set doesn't trigger an error, but missing quotes can!

Link to comment
Share on other sites

Actually, it does.

 

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

 

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

 

The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.

 

Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.

 

mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query.

[/code]

Link to comment
Share on other sites

The way I read it, it sounded as though you were implying a boolean value is never returned by mysql_query, which clearly isn't the case.

 

I've read the manual! The sentence I quoted previously is exactly what we're discussing about. While in the first part of it (mysql_query() doesn't return a boolean value), which I'm seeing now that it may be ambiguous, I was referring to the specific case mentioned in this thread. Anyway, it doesn't really matter.

 

Cheers

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.