简体   繁体   English

如何使用 PHP 获取 MySQL 表中的行数?

[英]How to get count of rows in MySQL table using PHP?

I simply want to use PHP to get a count of the total number of rows in a MySQL table, and store the number in a variable called $count .我只是想使用 PHP 来计算 MySQL 表中的总行数,并将该数字存储在名为$count的变量中。

I prefer procedural code since my mind doesn't work in object-oriented fashion.我更喜欢过程代码,因为我的思想不能以面向对象的方式工作。

$sql="SELECT COUNT(*) FROM news";
$result = mysqli_query($con, $sql);
$count = mysqli_fetch_assoc($result);
echo $count;

The above is the latest I tried.以上是我尝试过的最新版本。 Instead of giving me a number, it gives me the word "Array".它没有给我一个数字,而是给了我“数组”这个词。

You have a couple of options how to get the value of COUNT(*) from the SQL.您有几个选择如何从 SQL 获取COUNT(*)的值。 The easiest three are probably this:最简单的三个可能是这样的:

$sql = "SELECT COUNT(*) FROM news";
$result = mysqli_query($con, $sql);
$count = mysqli_fetch_assoc($result)['COUNT(*)'];
echo $count;

or using column alias:或使用列别名:

$sql = "SELECT COUNT(*) as cnt FROM news";
$result = mysqli_query($con, $sql);
$count = mysqli_fetch_assoc($result)['cnt'];
echo $count;

or using numerical array:或使用数值数组:

$sql = "SELECT COUNT(*) FROM news";
$result = mysqli_query($con, $sql);
$count = mysqli_fetch_row($result)[0];
echo $count;

Do not use mysqli_num_rows to count the records in the database as suggested in some places on the web.不要按照 web 上某些地方的建议使用mysqli_num_rows来统计数据库中的记录。 This function has very little use, and counting records is definitely not one of them.这个function用处很少,计数记录绝对不是其中之一。 Using mysqli_num_rows you would be asking MySQL to retrieve all matching records from database, which could be very resource consuming.使用mysqli_num_rows您将要求 MySQL 从数据库中检索所有匹配的记录,这可能非常耗费资源。 It is much better to delegate the job of counting records to MySQL and then just get the returned value in PHP as shown in my answer.最好将计数记录的工作委托给 MySQL,然后在 PHP 中获取返回值,如我的回答所示。

I would also recommend to learn OOP, which makes your code cleaner and easier to read.我还建议学习 OOP,它可以使您的代码更清晰,更易于阅读。 The same with OOP could be done as follows:与 OOP 相同,可以按如下方式进行:

$sql = "SELECT COUNT(*) FROM news";
$count = $con->query($sql)->fetch_row()[0];
echo $count;

If your query uses variables, then you could do a similar thing, but using prepared statements.如果您的查询使用变量,那么您可以做类似的事情,但使用准备好的语句。

$sql = "SELECT COUNT(*) FROM news WHERE category=?";
$stmt = $con->prepare($sql);
$stmt->bind_param('s', $category);
$stmt->execute();
$count = $stmt->get_result()->fetch_row()[0];
echo $count;

It's better to use COUNT(*) instead of selecting all rows and using mysqli_num_rows() .最好使用 COUNT(*) 而不是选择所有行并使用mysqli_num_rows()

Your basic idea was correct, you just needed to fetch the result properly.您的基本想法是正确的,您只需要正确获取结果即可。

$sql = "SELECT COUNT(*) FROM news";
$count = 0;
if ($result = mysqli_query($con, $sql)) {
    $row = mysqli_fetch_row($result);
    $count = $row[0];
}
echo $count;

The following PHP can be used to execute the query to calculate the amount of results:下面的PHP可以用来执行查询计算结果量:

$sql="SELECT COUNT(*) AS news_count FROM news";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_array($result);
$count = $row['news_count'];
echo $count;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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