php 计算行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18122428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Counting Number of Rows
提问by Obcure
I have the following code that used to echo the number of columns that was an accident and I wanted it to count rows now that I've changed it its not echoing anything. Any ideas? By the way I have already connected to the database even though it is not shown.
我有以下代码用于回显意外的列数,现在我希望它计算行数,因为我已经更改了它,它没有回显任何内容。有任何想法吗?顺便说一下,我已经连接到数据库,即使它没有显示。
<p>
<?php
$result = mysqli_query("SELECT * FROM User_info");
$num_rows = mysqli_num_rows($result);
echo $num_rows[0];
mysqli_close($con);
?>
records in our database start your search <a href="#">here</a> today.
</p>
回答by Sylvain Leroux
As a general rule, you should minimize the traffic between your DB server and your application server.
作为一般规则,您应该尽量减少数据库服务器和应用程序服务器之间的流量。
For your specific case, that means the recommended way of "counting" is:
对于您的具体情况,这意味着推荐的“计数”方式是:
<?php
$result = mysqli_query("SELECT count(*) FROM User_info");
$row = mysqli_fetch_row($result);
$num = $row[0];
?>
With that request (SELECT COUNT(*) ...
), the DB server will count rows for you. And just return that total -- possibly using indexesor internal data structure to return directly the result. With SELECT *
, the server has to retrieveand then sendall the data in your table. Quite expensive and rather inefficient since you don't use all these data after that in your application...
通过该请求 ( SELECT COUNT(*) ...
),数据库服务器将为您计算行数。只需返回该总数——可能使用索引或内部数据结构直接返回结果。使用SELECT *
,服务器必须检索并发送表中的所有数据。相当昂贵且效率低下,因为之后您不会在应用程序中使用所有这些数据......
EDIT -- if you want a "simple" explanation: what is the most efficient to know the number of pages in a book? To count them one by one, or to look at the page numbers in the footer?
编辑——如果你想要一个“简单”的解释:知道一本书的页数最有效的是什么?是一一数,还是看页脚中的页码?
With your initial solution (counting in PHP), it is like making a xerox of the book, and then counting the pages in the copy. With COUNT(*)
you let the DB server use the page numbers or even an index (the "TOC" of the book) to find the result.
使用您的初始解决方案(在 PHP 中计算),这就像对书进行复印,然后计算副本中的页数。随着COUNT(*)
你让DB服务器使用的页码,甚至是索引(书的“TOC”)找到的结果。
回答by Your Common Sense
Here is the only right way to count rows:
这是计算行数的唯一正确方法:
<?php
$result = mysqli_query("SELECT count(*) FROM User_info");
$row = mysqli_fetch_row($result);
$num = $row[0];
?>
<p>
<?=$num?> records in our database start your search <a href="#">here</a> today.
</p>
回答by Borniet
mysqli_num_rows() returns its result as an integer (or string if the value is too large for an integer), not as an array, so you need to change your code from:
mysqli_num_rows() 将其结果作为整数返回(如果该值对于整数来说太大,则返回字符串),而不是数组,因此您需要更改代码:
echo $num_rows[0];
to
到
echo $num_rows;
回答by ciruvan
Change that one line to
将这一行更改为
echo $num_rows;