Home >Database >Mysql Tutorial >Why Does My PHP Script Throw a 'mysql_fetch_array(): supplied argument is not a valid MySQL result' Warning?

Why Does My PHP Script Throw a 'mysql_fetch_array(): supplied argument is not a valid MySQL result' Warning?

Susan Sarandon
Susan SarandonOriginal
2024-12-16 18:39:11910browse

Why Does My PHP Script Throw a

Invalid Argument in mysql_fetch_array()

Problem Description

Upon executing a PHP script, the following error is encountered:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result [duplicate]

Origin of the Error

This error is typically caused when the $result variable, which is expected to contain a valid MySQL result set, is actually empty or invalid.

Code Snippet

The code snippet causing the issue:

$connector = new DbConnector();
$result = $connector->query('SELECT title,content FROM staff_vacancies ORDER BY ordering LIMIT 0,100');
while ($row = $connector->fetchArray($result)){

echo $row['title'].'</h3>';
echo $row['content'];
}

Possible Causes

Here are possible causes for the invalid $result variable:

  • Query Failure: The query executed using $connector->query() returned an empty result set or failed due to an error.
  • Syntax Error: The database query itself may contain syntax errors, causing it to fail.

Solution

To resolve this issue, consider the following steps:

  • Check for Query Errors: Use mysql_error() to check for errors in the query executed by $connector->query(). If an error occurred, it will be displayed.
  • Review the Query: Inspect the database query for any syntax or logical errors that may cause it to fail.
  • Enhanced Query Method: Refactor the query() method in DbConnector.php to handle error catching and provide a more descriptive error message.

The above is the detailed content of Why Does My PHP Script Throw a 'mysql_fetch_array(): supplied argument is not a valid MySQL result' Warning?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn