otuatail Posted May 18, 2011 Share Posted May 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236752-mysql_num_rows-returns-error-on-zero-rows/ Share on other sites More sharing options...
Fadion Posted May 18, 2011 Share Posted May 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236752-mysql_num_rows-returns-error-on-zero-rows/#findComment-1217023 Share on other sites More sharing options...
otuatail Posted May 18, 2011 Author Share Posted May 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236752-mysql_num_rows-returns-error-on-zero-rows/#findComment-1217045 Share on other sites More sharing options...
Fadion Posted May 18, 2011 Share Posted May 18, 2011 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'"; Quote Link to comment https://forums.phpfreaks.com/topic/236752-mysql_num_rows-returns-error-on-zero-rows/#findComment-1217172 Share on other sites More sharing options...
DavidAM Posted May 18, 2011 Share Posted May 18, 2011 $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 } Quote Link to comment https://forums.phpfreaks.com/topic/236752-mysql_num_rows-returns-error-on-zero-rows/#findComment-1217263 Share on other sites More sharing options...
Fadion Posted May 18, 2011 Share Posted May 18, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/236752-mysql_num_rows-returns-error-on-zero-rows/#findComment-1217268 Share on other sites More sharing options...
Pikachu2000 Posted May 18, 2011 Share Posted May 18, 2011 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] Quote Link to comment https://forums.phpfreaks.com/topic/236752-mysql_num_rows-returns-error-on-zero-rows/#findComment-1217275 Share on other sites More sharing options...
Fadion Posted May 18, 2011 Share Posted May 18, 2011 That's what I said: "instead it returns a resource identifier that can't be evaluated to true/false if there isn't any error in the query." Quote Link to comment https://forums.phpfreaks.com/topic/236752-mysql_num_rows-returns-error-on-zero-rows/#findComment-1217277 Share on other sites More sharing options...
Pikachu2000 Posted May 19, 2011 Share Posted May 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236752-mysql_num_rows-returns-error-on-zero-rows/#findComment-1217342 Share on other sites More sharing options...
Fadion Posted May 19, 2011 Share Posted May 19, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/236752-mysql_num_rows-returns-error-on-zero-rows/#findComment-1217365 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.