简体   繁体   中英

Counting the rows in a table in PHP

I want to count the number of rows in a table (using PHP and Mysql) all of the count() functions I can find only return columns, or array features, is there a function to return the number of rows? Many thanks!

you can count the number of rows returned by a query. note that this function is deprecated... it has options on the docs page here.

http://ca1.php.net/function.mysql-num-rows

You need to use mysqli_num_rows() :

$con=mysqli_connect("host","name","pass","database");

$sql="SELECT * FROM table_name";
$result=mysqli_query($con, $sql);
$rowcount=mysqli_num_rows($result);

$rowcount now stores the number of rows.

$count = mysql_num_rows($query);

将返回查询选择的行数,请确保您未在​​查询本身中使用count()

Depending if you are using mysql, mysqli or PDO query.

if mysql_query rkstar told you how to do it :

if mysqli_query : http://php.net/manual/fr/mysqli-stmt.num-rows.php

if PDO : do another query ! $rowCount = $db->query("SELECT FOUND_ROWS()")->fetchColumn();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM