Home  >  Article  >  Database  >  Why Does \"mysql_fetch_array() expects parameter 1 to be resource\" Error Occur?

Why Does \"mysql_fetch_array() expects parameter 1 to be resource\" Error Occur?

Barbara Streisand
Barbara StreisandOriginal
2024-11-02 11:59:02870browse

Why Does

Issue: Understanding "mysql_fetch_array() expects parameter 1 to be resource problem" Error

As mentioned in the potential duplicate provided, the error "mysql_fetch_array() expects parameter 1 to be resource problem" occurs when you attempt to use mysql_fetch_array() on a variable that is not a valid MySQL result resource.

Answer:

In your code, the issue arises from the mysql_query() call:

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

You should add error checking after the mysql_query() call to confirm that the query executed successfully. Here's the modified code:

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

If mysql_query() fails, it returns false (a boolean value), which will cause the problem with mysql_fetch_array() because it expects a mysql result object.

Now, you can safely use mysql_fetch_array($result) to iterate through the results. Remember to add similar error checking for other MySQL functions.

The above is the detailed content of Why Does \"mysql_fetch_array() expects parameter 1 to be resource\" Error Occur?. 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