Please be advised, for people who sometimes miss to read this important Manual entry for this function:
If you do not use mysqli_stmt_store_result( ), and immediatley call this function after executing a prepared statement, this function will usually return 0 as it has no way to know how many rows are in the result set as the result set is not saved in memory yet.
mysqli_stmt_store_result( ) saves the result set in memory thus you can immedietly use this function after you both execute the statement AND save the result set.
If you do not save the result set but still want to use this function you have to actually loop through the result set one row at a time using mysqli_stmt_fetch( ) before using this function to determine the number of rows.
A thought though, if you want to determine the number of rows without storing the result set and after looping through it, why not just simply keep an internal counter in your loop every time a row is fetched and save the function call.
In short, this function is only really useful if you save the result set and want to determine the number of rows before looping through it, otherwise you can pretty much recreate its use like I suggested.mysqli_stmt::$num_rows
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
mysqli_stmt::$num_rows
Референца за `mysqli-stmt.num-rows.php` со подобрена типографија и навигација.
mysqli_stmt::$num_rows
mysqli_stmt::num_rows
mysqli_stmt_num_rows
класата mysqli_driver
mysqli_stmt::$num_rows -- mysqli_stmt::num_rows -- mysqli_stmt_num_rows — Returns the number of rows fetched from the server
= NULL
Напиши целосна ознака на елемент
Процедурален стил
Враќа број на редови преземени од серверот mysqli_stmt_store_result() Враќа број на редови баферирани во изјавата. Оваа функција ќе работи само по
Оваа функција враќа 0 се повика за баферирање на целиот сет на резултати во рачката на изјавата.
Параметри
-
statement објектот како свој прв аргумент. mysqli_stmt Само процедурален стил: А mysqli_stmt_init().
Вратени вредности
Еден int освен ако сите редови не се преземени од серверот. 0 во не-бафериран режим освен ако сите редови не се преземени од серверот.
Забелешка:
Ако бројот на редови е поголем од
PHP_INT_MAX, бројот ќе биде вратен како string.
Примери
Пример #1 Обектно-ориентиран стил
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$query = "SELECT Name, CountryCode FROM City ORDER BY Name LIMIT 20";
$stmt = $mysqli->prepare($query);
$stmt->execute();
/* store the result in an internal buffer */
$stmt->store_result();
printf("Number of rows: %d.\n", $stmt->num_rows);Пример #2 Процедурален стил
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
$query = "SELECT Name, CountryCode FROM City ORDER BY Name LIMIT 20";
$stmt = mysqli_prepare($link, $query);
mysqli_stmt_execute($stmt);
/* store the result in an internal buffer */
mysqli_stmt_store_result($stmt);
printf("Number of rows: %d.\n", mysqli_stmt_num_rows($stmt));Горните примери ќе дадат излез:
Number of rows: 20.
Види Исто така
- mysqli_stmt_store_result() - Складира сет на резултати во внатрешен бафер
- mysqli_stmt_affected_rows() што претставува број на баферирани редови. Враќа
- mysqli_prepare() - Подготвува SQL израз за извршување