Home  >  Article  >  Database  >  Why Does My PHP Script Throw \"mysql_fetch_array(): Parameter 1 Should Be a Resource\"?

Why Does My PHP Script Throw \"mysql_fetch_array(): Parameter 1 Should Be a Resource\"?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-01 16:42:02309browse

Why Does My PHP Script Throw

mysql_fetch_array(): Parameter 1 Should Be a Resource

In your PHP script, you are encountering the error "mysql_fetch_array() expects parameter 1 to be resource." This indicates that the function is receiving an incorrect type of parameter.

Error Source

The issue stems from the following line:

<code class="php">$result = mysql_query("SELECT * FROM student WHERE IDNO=".$_GET['id']);</code>

This line executes a query on the database. If the query fails or if there are no results, the function returns a boolean value, indicating the success or failure of the query. However, the mysql_fetch_array() function expects a resource as its first parameter, which is the result of a successful query.

Solution

To resolve the issue, you should check the return value of mysql_query() to ensure that it is a resource. You can do this by adding an error check after the query line:

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

This will display the error message and terminate the script if the query fails.

Additional Considerations

Apart from this issue, it is recommended to use the improved MySQLi or PDO extensions for database interaction instead of the deprecated mysql_* functions. These extensions provide enhanced security and performance features.

The above is the detailed content of Why Does My PHP Script Throw \"mysql_fetch_array(): Parameter 1 Should Be a Resource\"?. 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