Home  >  Article  >  Database  >  Why does \"mysql_fetch_array() expects parameter 1 to be resource problem\" error occur, and how to resolve it?

Why does \"mysql_fetch_array() expects parameter 1 to be resource problem\" error occur, and how to resolve it?

DDD
DDDOriginal
2024-11-02 12:53:30430browse

Why does

"Mysql_fetch_array() expects parameter 1 to be resource problem [duplicate]" - Understanding and Resolving the Error

The error "mysql_fetch_array() expects parameter 1 to be resource problem" arises when the mysql_fetch_array() function does not receive a valid resource as the first argument. This typically occurs when the preceding mysql_query() call fails to retrieve data from the database.

To resolve this error, it is crucial to perform error checking after the mysql_query() call. The following example demonstrates how to add error checking to the provided code:

<br>$result = mysql_query("SELECT * FROM student WHERE IDNO=" . $_GET['id']);<br>if (!$result) { // Add this error checking.</p>
<pre class="brush:php;toolbar:false">die('Invalid query: ' . mysql_error());

}

When mysql_query() fails, it returns false, a boolean value. If you pass it to mysql_fetch_array(), expecting a mysql result object, you will encounter the stated error.

Here's the rewritten code with error checking:

<br>$con = mysql_connect("localhost", "root", "nitoryolai123$%^");<br>if (!$con) {</p>
<pre class="brush:php;toolbar:false">die('Could not connect: ' . mysql_error());

}

mysql_select_db("school", $con);
$result = mysql_query("SELECT * FROM student WHERE IDNO=" . $_GET['id']);
if (!$result) {

die('Invalid query: ' . mysql_error());

}

while ($row = mysql_fetch_array($result)) {

?>
<table class="a" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#D3D3D3">
    <tr>
        <form name="formcheck" method="get" action="updateact.php" onsubmit="return formCheck(this);">
            <td>
                <table border="0" cellpadding="3" cellspacing="1" bgcolor="">
                    <tr>
                        <td colspan="16" height="25" style="background:#5C915C; color:white; border:white 1px solid; text-align: left"><strong><font size="2">Update Students</font></strong></td>
                    </tr>
                    <tr>
                        <td width="30" height="35"><font size="2">*I D Number:</font></td>
                        <td width="30"><input name="idnum" onkeypress="return isNumberKey(event)" type="text" maxlength="5" id='numbers' value="<?php echo $_GET['id']; ?>" /></td>
                    </tr>
                    <tr>
                        <td width="30" height="35"><font size="2">*Year:</font></td>
                        <td width="30"><input name="yr" onkeypress="return isNumberKey(event)" type="text" maxlength="5" id='numbers' value="<?php echo $row["YEAR"]; ?>" /></td>
                    </tr>
                </table>
            </td>
        </form>
    </tr>
</table>
<?php

}

By incorporating error checking into your code, you can now identify and handle potential database retrieval failures effectively, preventing the "mysql_fetch_array() expects parameter 1 to be resource problem" error.

The above is the detailed content of Why does \"mysql_fetch_array() expects parameter 1 to be resource problem\" error occur, and how to resolve it?. 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