hi im a newbie at this but im getting a error message that says:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/pickurau/public_html/classes/Lib/Query.php on line 27
and line 27 starts with the if(!mysql_affected_rows() || mysql_num_rows($this->rs) < 1)
return false;
does anyone know how can i fix this error?
heres the script code:

<?php
class Lib_Query extends Lib_DbConnect 
{
	var $rs;
	var $totrows;
	var $records;
	
	
	/**
	 * Enter description here...
	 *
	 * @param string $sql
	 * @param array $fields
	 * @return boolean
	 */
	function executeQuery($sql, $fields = array())
	{
		//echo $sql,"<br/>";
		if(substr_count($sql,'#')!=count($fields))
			return false;
		if(count($fields)>0)
			$sql = $this->makeQuery($sql,$fields);	// Security::makeQuery();
		$i=0;

		$this->rs = mysql_query($sql);

		if(!mysql_affected_rows() || mysql_num_rows($this->rs) < 1)
			return false;
		else
		{
			$this->totrows = mysql_num_rows($this->rs);
			while($fetch = mysql_fetch_array($this->rs))
			{
				$this->records[$i] = $fetch;
				$i++;
			}
			for($i=0;$i<count($this->records);$i++)
			{
				foreach ($this->records[$i] as $key=>$item)
				{
					if(is_numeric($key))
						unset($this->records[$i][$key]);
				}
			}
			return true;
		}
	}

	/**
	 * @param string $sql
	 * @return boolean
	 */
	function updateQuery($sql, $fields=array())
	{
	  
		if(substr_count($sql,'#')!=count($fields))
			return false;
		if(count($fields)>0)
			$sql = $this->makeQuery($sql,$fields);	// Security::makeQuery();

		$this->rs = mysql_query($sql);
		if(!$this->rs)
			return false;
		else
			return true;
	}	
}
?>

Dani AI

Generated

Quick diagnosis: that warning means mysql_num_rows() was given something that is not a MySQL result resource. The usual causes are (a) mysql_query() failed and returned FALSE, or (b) the query was a non-SELECT (so mysql_query() returned TRUE, not a resource). was right — show the actual SQL so you can see why it fails. (php.net)

Immediate, practical fixes to add to executeQuery():

  • Don’t call mysql_num_rows() until you confirm the query returned a result resource.
  • Right after mysql_query($sql) check for FALSE and log mysql_error() and the SQL for debugging.
  • Distinguish SELECT results (resource) from non-SELECT (boolean true). Example pattern:
$this->rs = mysql_query($sql);
if ($this->rs === false) {
    error_log('MySQL error: '.mysql_error().' SQL: '.$sql);
    return false;
}
if (!is_resource($this->rs)) {
    // non-SELECT: success is reflected by mysql_affected_rows()
    return (mysql_affected_rows() > 0);
}
if (mysql_num_rows($this->rs) < 1) {
    return false;
}

Use mysql_error() to see SQL problems and use mysql_num_rows() only when you actually have a result resource. Those checks avoid the warning and point you at the real cause. (php.net)

A couple of notes tied to the thread: was on the right track about SELECT vs. DML, and ’s idea to make $this->rs an array is not correct here — $this->rs should hold the result resource from mysql_query(). Also remove the combined check if(!mysql_affected_rows() || mysql_num_rows($this->rs) < 1) and replace it with the explicit guards above; mysql_affected_rows() is intended for INSERT/UPDATE/DELETE. (php.net)

Longer term: the ext/mysql API is deprecated/removed in modern PHP — plan to migrate to MySQLi or PDO and use prepared statements to avoid these kinds of errors and SQL-injection risks. (php.net)

Recommended Answers

All 6 Replies

The query is failing, post the query and we might be able to fix it.

what do you mean by post the query? this is the query.php file where its saying the error is its

<?php
class Lib_Query extends Lib_DbConnect
{
var $rs;
var $totrows;
var $records;


/**
* Enter description here...
*
* @param string $sql
* @param array $fields
* @return boolean
*/
function executeQuery($sql, $fields = array())
{
//echo $sql,"<br/>";
if(substr_count($sql,'#')!=count($fields))
return false;
if(count($fields)>0)
$sql = $this->makeQuery($sql,$fields); // Security::makeQuery();
$i=0;

$this->rs = mysql_query($sql);

if(!mysql_affected_rows() || mysql_num_rows($this->rs) < 1)
return false;
else
{
$this->totrows = mysql_num_rows($this->rs);
while($fetch = mysql_fetch_array($this->rs))
{
$this->records[$i] = $fetch;
$i++;
}
for($i=0;$i<count($this->records);$i++)
{
foreach ($this->records[$i] as $key=>$item)
{
if(is_numeric($key))
unset($this->records[$i][$key]);
}
}
return true;
}
}

/**
* @param string $sql
* @return boolean
*/
function updateQuery($sql, $fields=array())
{

if(substr_count($sql,'#')!=count($fields))
return false;
if(count($fields)>0)
$sql = $this->makeQuery($sql,$fields); // Security::makeQuery();

$this->rs = mysql_query($sql);
if(!$this->rs)
return false;
else
return true;
}
}
?>

I mean post the query. There is a query being passed to the 'executeQuery' function that is incorrect, that is why the function is failing.

mysql_num_rows:
Retrieves the number of rows from a result set. This command is only valid for statements like SELECT or SHOW that return an actual result set. To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use mysql_affected_rows().

My guess would be that you're passing a fetched result like an array of the query.

in line 25: $this->rs[] = mysql_query($sql);
put a [] after the rs.
or try to declare your var $rs = array();

or that is an array so maybe you get the sizeof the array.

$this->totrows = mysql_num_rows(sizeof($this->rs));

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.