PHP Warning: mysql_num_rows() expects parameter solution

PHPz
Release: 2023-06-24 22:08:01
Original
1579 people have browsed it

PHP is a widely used open source scripting language used for web development, where many web applications (such as WordPress) rely on the MySQL database to store and retrieve data. However, when using the mysql_num_rows() function, the following warning sometimes appears:

PHP Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in...

In this article , we will discuss the causes of this problem and how to solve it.

What is the mysql_num_rows() function?

The mysql_num_rows() function is used to get the number of rows retrieved from the MySQL query. It accepts a result set resource as a parameter and returns the number of rows or false if an error occurs. Here is an example:

$result = mysql_query("SELECT * FROM mytable");
$num_rows = mysql_num_rows($result);
echo "Rows: ".$num_rows;

This will return the number of rows retrieved from the mytable table. However, if the result set is not a resource, a warning will appear:

PHP Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in...

Why "boolean given" appears "warn?

This problem is usually caused by a failed SQL query. When using the mysql_query() function, it may return false, indicating that the query failed. If we pass this result as a parameter to the mysql_num_rows() function, it will be interpreted as a boolean value instead of the expected result set resource. Therefore, the warning "PHP Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in ..." appears on the console.

How to solve the "boolean given" warning?

In order to solve this problem, we need to ensure that the SQL query is successful before using the mysql_num_rows() function. We can check the results of the mysql_query() function like this:

$result = mysql_query("SELECT * FROM mytable");
if($result){
$num_rows = mysql_num_rows($result );
echo "Rows: ".$num_rows;
}

This will ensure that the mysql_num_rows() function is only used after the query has been successfully executed.

Another method is to use the MySQLi extension or PDO (PHP Data Objects) to connect and query the MySQL database. The MySQLi extension provides an improved method of accessing MySQL databases, while PDO can be used to connect and query different types of databases (such as MySQL, PostgreSQL, SQLite).

The following is an example using MySQLi:

$conn = mysqli_connect("localhost","username","password","dbname");
if (!$conn ) {
die("Could not connect: ".mysqli_error($conn));
}

$result = mysqli_query($conn,"SELECT * FROM mytable");
if($result){
$num_rows = mysqli_num_rows($result);
echo "Rows: ".$num_rows;
}

In addition, PDO also provides an easy-to-use And secure database access method:

$conn = new PDO("mysql:host=localhost;dbname=myDB","username","password");
$result = $conn-> ;query("SELECT * FROM mytable");
if($result){
$num_rows = $result->rowCount();
echo "Rows: ".$num_rows;
}

Summary

The "boolean given" warning when using the mysql_num_rows() function is usually caused by a failed SQL query. In order to solve this problem, we need to ensure that the query is successful before using the mysql_num_rows() function, or use a different MySQL database access method, such as MySQLi extension or PDO.

The above is the detailed content of PHP Warning: mysql_num_rows() expects parameter solution. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
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!