"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > 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\"?

Published on 2024-11-11
Browse:816

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:

$result = mysql_query("SELECT * FROM student WHERE IDNO=".$_GET['id']);

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:

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

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.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3