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> <div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><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> <div class="code" style="position:relative; padding:0px; margin:0px;"><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!