简体   繁体   English

如何从数据库中获取返回的行数

[英]How to get number of returned rows from a database

After I perform a database query, and some rows are echoed, it seems that I can't get the number of rows which are shown, after the query. 在我执行数据库查询并且回显了一些行之后,似乎在查询之后我无法获得显示的行数。 Tried to use mysql_num_rows() but my $result is like this: 试图使用mysql_num_rows()但我的$ result是这样的:

$result = $conn->query($sql) or die();

so I think that the problem is that I've used the built-in MySQLi() class, and for some reason mysql_num_rows() is not working in accordance with this $result. 所以我认为问题是我使用了内置的MySQLi()类,并且由于某种原因,mysql_num_rows()没有按照这个$ result工作。 How can I get it to work with the current $result I'm using, or is there any other way to return the number of rows using the MySQLi() class to create the $result?? 我怎样才能使用我正在使用的当前$ result,或者是否有任何其他方法可以使用MySQLi()类来返回行数以创建$ result?

mysql and mysqli are NOT interchangeable. mysql和mysqli不可互换。 They're two completely different modules, maintain their own separate connections, and are results/handles from one are NOT useable in the other. 它们是两个完全不同的模块,保持它们各自的连接,并且一个的结果/句柄在另一个中是不可用的。 They may both use the same underlying mysql libraries and talk to the same database, but they're utterly independent of each other. 它们可能都使用相同的底层mysql库并与同一个数据库通信,但它们完全相互独立。

If you're using MySQLi, then stick with MySQLi functions, which for your problem would be to use http://php.net/manual/en/mysqli-stmt.num-rows.php 如果您正在使用MySQLi,那么坚持使用MySQLi函数,对于您的问题,可以使用http://php.net/manual/en/mysqli-stmt.num-rows.php

Note1: If you only need the number of rows in your table, it is better to do the folowing: 注意1:如果您只需要表中的行数,则最好执行以下操作:

$result = $conn->query("SELECT COUNT(*) FROM `table`");
$row = $result->fetch_row();
echo '#: ', $row[0];

Note 2: Don't mix up mysqli_field_count and mysqli_stmt_num_rows . 注2:不要混淆mysqli_field_countmysqli_stmt_num_rows For example : 例如 :

id firstname
1  foo
2  bar
3  baz
  • field_count is 2 field_count是2
  • num_rows is 3 num_rows是3

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

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