Home > Database > Mysql Tutorial > body text

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

DDD
Release: 2024-11-02 12:53:30
Original
429 people have browsed it

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>
<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());
Copy after login
Copy after login

}

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());
Copy after login

}

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

die('Invalid query: ' . mysql_error());
Copy after login
Copy after login

}

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
Copy after login

}

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!

source:php.cn
Previous article:Why Can\'t I Load Data into MySQL? Troubleshooting \'LOAD DATA\' Permission Errors Next article:Why Does Inserting 0 into a BIT(1) Field in MySQL using PDO Result in 1?
Statement of this Website
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
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!