©
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
(PHP 5)
mysqli_result::$num_rows -- mysqli_num_rows — Gets the number of rows in a result
面向对象风格
过程化风格
$result
)Returns the number of rows in the result set.
The behaviour of mysqli_num_rows() depends on whether buffered or unbuffered result sets are being used. For unbuffered result sets, mysqli_num_rows() will not return the correct number of rows until all the rows in the result have been retrieved.
result
仅以过程化样式:由 mysqli_query() , mysqli_store_result() 或 mysqli_use_result() 返回的结果集标识。
Returns number of rows in the result set.
Note:
If the number of rows is greater than
PHP_INT_MAX
, the number will be returned as a string.
Example #1 面向对象风格
<?php
$mysqli = new mysqli ( "localhost" , "my_user" , "my_password" , "world" );
if ( mysqli_connect_errno ()) {
printf ( "Connect failed: %s\n" , mysqli_connect_error ());
exit();
}
if ( $result = $mysqli -> query ( "SELECT Code, Name FROM Country ORDER BY Name" )) {
$row_cnt = $result -> num_rows ;
printf ( "Result set has %d rows.\n" , $row_cnt );
$result -> close ();
}
$mysqli -> close ();
?>
Example #2 过程化风格
<?php
$link = mysqli_connect ( "localhost" , "my_user" , "my_password" , "world" );
if ( mysqli_connect_errno ()) {
printf ( "Connect failed: %s\n" , mysqli_connect_error ());
exit();
}
if ( $result = mysqli_query ( $link , "SELECT Code, Name FROM Country ORDER BY Name" )) {
$row_cnt = mysqli_num_rows ( $result );
printf ( "Result set has %d rows.\n" , $row_cnt );
mysqli_free_result ( $result );
}
mysqli_close ( $link );
?>
以上例程会输出:
Result set has 239 rows.
[#1] justso at inbox dot ru [2015-11-14 09:36:38]
in php 5.3.8 had unexpected troubles when checking for mysqli_result::$num_rows
If the result of the query is empty then var_dump of the result will be like this:
class mysqli_result#5 (5) {
public $current_field => NULL
public $field_count => NULL
public $lengths => NULL
public $num_rows => NULL
public $type => NULL
}
but var_dump($result->num_rows) will give integer-typed zero instead of NULL:
int(0)
[#2] davideramondetti at yahoo dot it [2015-04-19 18:42:22]
use this agent. it 's easy:
<?php
// to have the actions code send me an email
$mysqli_rows = InfornBlog_AskInjectionMySQLICount($result); // Start Rows Injection for $result
?>
or you can use my another function(included in crypt code), ask(), for ask to mysql db the num of the rows for a fast result view. You can use ask() for all commands of PHP:
<?php
// to have the actions code send me an email
// COMMAND + THE VALUE
$mysqli_rows = ask(InfornBlog_AskInjectionMySQLICount($result)); // Start Rows Injection for $result
$print_it = ask(print $mysqli_rows); // Print the result
?>
This function is so easy. You must remember don' t delete the crypted code.
[#3] Anonymous [2014-11-26 12:56:05]
rtytryrytrytrytryrtytrytrygdfhhfggfhgfhgf
[#4] borisigna [2011-08-07 20:19:20]
If you have problems making work this num_rows, you have to declare ->store_result() first.
<?php
$mysqli = new mysqli("localhost","root", "", "tables");
$query = $mysqli->prepare("SELECT * FROM table1");
$query->execute();
$query->store_result();
$rows = $query->num_rows;
echo $rows;
// Return 4 for example
?>