mysqli_stmt::$num_rows

mysqli_stmt::num_rows

mysqli_stmt_num_rows

(PHP 5, PHP 7)

mysqli_stmt::$num_rows -- mysqli_stmt::num_rows -- mysqli_stmt_num_rowsВозвращает число строк в результате запроса

Описание

Объектно-ориентированный стиль

public mysqli_stmt::num_rows(): int|string

Процедурный стиль

mysqli_stmt_num_rows(mysqli_stmt $statement): int|string

Возвращает число строк в результате запроса. Результат выполнения mysqli_stmt_num_rows() зависит от того использовалась ли mysqli_stmt_store_result() для буферизации результата выборки в дескрипторе.

Если используется mysqli_stmt_store_result(), mysqli_stmt_num_rows() может быть вызвана в тот же момент.

Список параметров

stmt

Только для процедурного стиля: Объект mysqli_stmt, полученный с помощью mysqli_stmt_init().

Возвращаемые значения

Целое число, отражающее число строк в результате запроса.

Примеры

Пример #1 Объектно-ориентированный стиль

<?php
/* Открыть соединение */
$mysqli = new mysqli("localhost""my_user""my_password""world");

/* Проверить соединение */
if (mysqli_connect_errno()) {
    
printf("Ошибка соединения: %s\n"mysqli_connect_error());
    exit();
}

$query "SELECT Name, CountryCode FROM City ORDER BY Name LIMIT 20";
if (
$stmt $mysqli->prepare($query)) {

    
/* Выполнить запрос */
    
$stmt->execute();

    
/* Сохранить результат */
    
$stmt->store_result();

    
printf("Число строк: %d.\n"$stmt->num_rows);

    
/* Закрыть выражение */
    
$stmt->close();
}

/* Закрыть соединение */
$mysqli->close();
?>

Пример #2 Процедурный стиль

<?php
/* Открыть соединение */
$link mysqli_connect("localhost""my_user""my_password""world");

/* Проверить соединение */
if (mysqli_connect_errno()) {
    
printf("Ошибка соединения: %s\n"mysqli_connect_error());
    exit();
}

$query "SELECT Name, CountryCode FROM City ORDER BY Name LIMIT 20";
if (
$stmt mysqli_prepare($link$query)) {

    
/* Выполнить запрос */
    
mysqli_stmt_execute($stmt);

    
/* Сохранить результат */
    
mysqli_stmt_store_result($stmt);

    
printf("Число строк: %d.\n"mysqli_stmt_num_rows($stmt));

    
/* Закрыть выражение */
    
mysqli_stmt_close($stmt);
}

/* Закрыть соединение */
mysqli_close($link);
?>

Результат выполнения данных примеров:

Число строк: 20.

Смотрите также

  • mysqli_stmt_affected_rows() - Возвращает общее количество строк, изменённых, удалённых или добавленных последним выполненным выражением
  • mysqli_prepare() - Подготавливает SQL выражение к выполнению
  • mysqli_stmt_store_result() - Передаёт результирующий набор запроса на клиента

add a note add a note

User Contributed Notes 1 note

up
74
Typer85 at gmail dot com
17 years ago
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.
To Top