本教程操作环境:windows7系统、php7.1版,dell g3电脑
php查询语句,有两种方法获得查询的总记录数。
mysqli 面向过程的方式
$sql = "select id, firstname, lastname from myguests";$result = mysqli_query($conn, $sql); num_rows=mysqli_num_rows($result);
一是使用mysql_num_rows函数,例子代码:
<?php$link = mysql_connect("localhost", "mysql_user", "mysql_password");mysql_select_db("database", $link); $result = mysql_query("select * from table1", $link);$num_rows = mysql_num_rows($result);echo "$num_rows rows\n";?>
二是修改查询语句,使用count(*)作为查询内容,例子代码:
<?php$link = mysql_connect("localhost", "mysql_user", "mysql_password");mysql_select_db("database", $link); $result = mysql_query("select count(*) from table1", $link);list($num_rows) = mysql_fetch_row($result);echo "$num_rows rows\n";?>
推荐:《2021年php面试题大汇总(收藏)》《php视频教程》
以上就是php如何查询返回记录数的详细内容。
