计算mysql中的行数?

发布于 2024-11-29 09:39:58 字数 319 浏览 2 评论 0原文

如何用php统计mysql数据库表的行数?

如果数据库中有 5 行,它们的编号显示如下(粗体):

<块引用>

所有列是:5

1 行 1
2 行 2
3 行 3
4 行 4
5 第5行

How to count the number of rows in mysql database tables with php?

if have in database 5 rows, they number show like this(bold):

all columns is: 5

1 row1
2 row2
3 row3
4 row4
5
row5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

影子是时光的心 2024-12-06 09:39:58

只需使用这个 SQL 查询:

SELECT COUNT(*) FROM table_name

如果您想知道另一个查询返回了多少行,您可以使用 PHP mysql_num_rows 函数。 (您也可以为处理的每一行增加一个计数器,但如果您需要在枚举结果之前知道记录数,则此函数会很方便。)

Just use this SQL query:

SELECT COUNT(*) FROM table_name

If you want to know how many rows were returned by another query, you can use the PHP mysql_num_rows function. (You could also just increment a counter for each row you process, but this function is handy if you need to know the number of records prior to enumerating the results.)

水中月 2024-12-06 09:39:58

我们如何将它们重新算在一起? LIKE: 1 2 3 4 5. 我不想在列数据库中使用 id

select list_of_fields,@rn:=@rn+1 as row_num
from table,(select @rn:=0) as r order by id

How do we count them back together? LIKE: 1 2 3 4 5. i not want use of id in column database

select list_of_fields,@rn:=@rn+1 as row_num
from table,(select @rn:=0) as r order by id
笑叹一世浮沉 2024-12-06 09:39:58

您可以使用此

$result = $this->db->get(<table_name>);
$num_rows = $result->num_rows();

$num_rows 作为 table_name 中的总行数

然后您可以执行以下操作

echo 'The number of rows in table_name is '.$num_rows< /代码>;

You can use this

$result = $this->db->get(<table_name>);
$num_rows = $result->num_rows();

$num_rows would be the total rows in table_name

Then you can just do this

echo 'The number of rows in table_name is '.$num_rows;

野却迷人 2024-12-06 09:39:58

这个查询应该可以满足您的需要:

SELECT COUNT(*) as count FROM `table`

结果集将是

[count]
-------
5

假设您有 5 行。


为了手动计算每一行并显示其索引(不使用 ID),我会做类似的事情

$counter = 1;
$stmt = $db->prepare('SELECT `field` FROM `table`');
$stmt->execute();
while($row = $stmt->fetch()) {
    echo "<b>{$counter}:</b> {$row['field']}";
    $counter++;
}

This query should do the trick for you:

SELECT COUNT(*) as count FROM `table`

The result set would be

[count]
-------
5

Assuming you have 5 rows.


In order to manually count each row and display its index (without using ID), I would do something like

$counter = 1;
$stmt = $db->prepare('SELECT `field` FROM `table`');
$stmt->execute();
while($row = $stmt->fetch()) {
    echo "<b>{$counter}:</b> {$row['field']}";
    $counter++;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文