Home >Database >Mysql Tutorial >Why Does \'mysql_fetch_array() Expects Parameter 1 to Be Resource\' Error Occur, and How Can It Be Fixed?

Why Does \'mysql_fetch_array() Expects Parameter 1 to Be Resource\' Error Occur, and How Can It Be Fixed?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-02 21:34:30961browse

Why Does

mysql_fetch_array() Expects Parameter 1 to Be Resource Problem

This error occurs when the mysql_fetch_array() function is called with an invalid or non-existent resource as its first parameter. A resource in PHP is a special type of variable that represents an external resource, such as a database connection or a file handle.

In the provided code, the mysql_query() function is used to retrieve a result set from a database. If the query execution fails, the function will return false, which is a boolean value. When a boolean value is passed to mysql_fetch_array(), it will cause the error "mysql_fetch_array() expects parameter 1 to be resource."

To resolve this issue, add an error check after the mysql_query() call:

<code class="php">$result = mysql_query("SELECT * FROM student WHERE IDNO=".$_GET['id']);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}</code>

If mysql_query() returns false, the error check will trigger and an error message will be displayed. With this error checking in place, the mysql_fetch_array() function will only be called if a valid result set has been retrieved from the database.

The above is the detailed content of Why Does \'mysql_fetch_array() Expects Parameter 1 to Be Resource\' Error Occur, and How Can It Be Fixed?. 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