Home  >  Article  >  Backend Development  >  PHP Warning: Solution to mysql_fetch_array() expects parameter

PHP Warning: Solution to mysql_fetch_array() expects parameter

WBOY
WBOYOriginal
2023-06-22 09:41:381154browse

PHP Warning: mysql_fetch_array() expects parameter - Reasons for the warning message and solutions

In PHP, when we use the mysql_fetch_array() function, sometimes such a warning message will appear: PHP Warning : mysql_fetch_array() expects parameter. This warning message is very common, but it is actually easy to solve. In this article, I will analyze the reasons for this warning message and provide corresponding solutions.

PHP Warning: mysql_fetch_array() expects parameter - the reason for the warning message

When using the mysql_fetch_array() function, we need to pass a parameter - the resource identifier of the query result (result identifier ). This resource identifier is returned after querying data using the mysql_query() function. If we do not pass this resource identifier correctly, the PHP Warning: mysql_fetch_array() expects parameter warning message will appear.

The following is a sample code in which this warning message appears:

<?php  
// 连接数据库  
$link = mysql_connect('localhost', 'root', '123456') or die('Could not connect: ' . mysql_error());  
  
// 选择数据库  
mysql_select_db('test', $link) or die('Could not select database');  
  
// 执行查询  
$result = mysql_query('SELECT * FROM `user`');  
  
// 遍历结果集  
while($row = mysql_fetch_array()) {  
    // 输出每行数据  
    echo $row['username'] . "    " . $row['password'] . "
";  
}  
  
// 释放查询结果  
mysql_free_result($result);  
  
// 关闭数据库连接  
mysql_close($link);  
?>

In the above code, the mysql_fetch_array() function does not pass any parameters, so this warning message appears.

How to solve PHP Warning: mysql_fetch_array() expects parameter - warning message

The way to solve this problem is very simple, just pass the resource identifier of the query result in the mysql_fetch_array() function . In our sample code, we only need to change the call of this function to the following form:

while($row = mysql_fetch_array($result)) {  
    // 输出每行数据  
    echo $row['username'] . "    " . $row['password'] . "
";  
} 

The current code has correctly passed the resource identifier, so this warning message will no longer appear.

In addition, it is also recommended that you do not use the mysql_ functions because they have been marked as obsolete functions. Instead, there are mysqli_ functions and PDO (PHP Data Objects).

Summary

PHP Warning: mysql_fetch_array() expects parameter - The warning message is caused by not correctly passing the parameter of the mysql_fetch_array() function - the resource identifier of the query result. To solve this problem, just pass this resource identifier in the function call. At the same time, it is also recommended that you use more modern and secure database access methods, such as mysqli_* functions and PDO.

The above is the detailed content of PHP Warning: Solution to mysql_fetch_array() expects parameter. 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