mysqli_fetch_row
Fetches one row of data from the result set and returns it as an enumerated array, where each column is stored in an array offset starting from 0 (zero). Each subsequent call to this function will return the next row within the result set, or null if there are no more rows.
Note: This function sets NULL fields to the PHP null value.
Parameters
Return Values
Returns an enumerated array representing the fetched row, null if there are no more rows in the result set, or false on failure.
Examples
Example #1 mysqli_result::fetch_row() example
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 ID DESC» ;
$result = $mysqli -> query ( $query );
/* fetch object array */
while ( $row = $result -> fetch_row ()) printf ( «%s (%s)\n» , $row [ 0 ], $row [ 1 ]);
>
mysqli_report ( MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT );
$mysqli = mysqli_connect ( «localhost» , «my_user» , «my_password» , «world» );
$query = «SELECT Name, CountryCode FROM City ORDER BY ID DESC» ;
$result = mysqli_query ( $mysqli , $query );
/* fetch associative array */
while ( $row = mysqli_fetch_row ( $result )) printf ( «%s (%s)\n» , $row [ 0 ], $row [ 1 ]);
>
The above examples will output something similar to:
Pueblo (USA) Arvada (USA) Cape Coral (USA) Green Bay (USA) Santa Clara (USA)
See Also
- mysqli_fetch_array() — Fetch the next row of a result set as an associative, a numeric array, or both
- mysqli_fetch_assoc() — Fetch the next row of a result set as an associative array
- mysqli_fetch_column() — Fetch a single column from the next row of a result set
- mysqli_fetch_object() — Fetch the next row of a result set as an object
- mysqli_query() — Performs a query on the database
- mysqli_data_seek() — Adjusts the result pointer to an arbitrary row in the result
User Contributed Notes 4 notes
It’s worth noting that the MySQLi functions (and, I presume, the MySQL functions) fetch a string regardless of the MySQL data type. E.g. if you fetch a row with an integer column, the corresponding value for that column and row will still be stored as a string in the array returned by mysql_fetch_row.
Note that mysqli_fetch() is deprecated but still is in PHP function list. mysqli_fetch_row() is nowadays mysql procedural style used, but is not listed in PHP functions.
Remember that fetch() and fetch_row() are two different things, and differ in the way to use them.
— fetch() is used on a statement (like an executed prepared statement) and needs to be used in association with bind_result().
— fetch_row() is used on a result (like the result of query()).
As a consequence, if you want to use to use fetch_row() with an executed prepared statement, first you’ll have to get the result out of this statement with mysqli_store_result() or mysqli_use_result().
4. Be careful when porting from ext/mysql to ext/mysqli. The following
functions return NULL when no more data is available in the result set
(ext/mysql’s functions return FALSE).
— mysqli_fetch_row()
— mysqli_fetch_array()
— mysqli_fetch_assoc()
mysqli_fetch_row
Выбирает одну строку данных из результирующего набора и возвращает ее в виде массива, в котором индексы элементов соответствуют номерам столбцов (начиная с 0). Каждый последующий вызов функции будет возвращать массив с данными следующей строки набора или NULL , если строки закончились.
Список параметров
Только для процедурного стиля: Идентификатор результата запроса, полученный с помощью mysqli_query() , mysqli_store_result() или mysqli_use_result() .
Возвращаемые значения
mysqli_fetch_row() возвращает массив строк, соответствующих данным в выбранной строке результирующей таблицы, или NULL , если доступных строк больше нет.
Замечание: Эта функция устанавливает NULL-поля в значение NULL PHP.
Примеры
Пример #1 Объектно-ориентированный стиль
$mysqli = new mysqli ( «localhost» , «my_user» , «my_password» , «world» );
?php
/* проверка подключения */
if ( mysqli_connect_errno ()) printf ( «Не удалось подключиться: %s\n» , mysqli_connect_error ());
exit();
>
$query = «SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5» ;
if ( $result = $mysqli -> query ( $query ))
/* выборка данных и помещение их в массив */
while ( $row = $result -> fetch_row ()) printf ( «%s (%s)\n» , $row [ 0 ], $row [ 1 ]);
>
/* очищаем результирующий набор */
$result -> close ();
>
/* закрываем подключение */
$mysqli -> close ();
?>
Пример #2 Процедурный стиль
$link = mysqli_connect ( «localhost» , «my_user» , «my_password» , «world» );
?php
/* проверка подключения */
if ( mysqli_connect_errno ()) printf ( «Не удалось подключиться: %s\n» , mysqli_connect_error ());
exit();
>
$query = «SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5» ;
if ( $result = mysqli_query ( $link , $query ))
/* выборка данных и помещение их в массив */
while ( $row = mysqli_fetch_row ( $result )) printf ( «%s (%s)\n» , $row [ 0 ], $row [ 1 ]);
>
/* очищаем результирующий набор */
mysqli_free_result ( $result );
>
/* закрываем подключение */
mysqli_close ( $link );
?>
Результат выполнения данных примеров:
Pueblo (USA) Arvada (USA) Cape Coral (USA) Green Bay (USA) Santa Clara (USA)
Смотрите также
- mysqli_fetch_array() — Выбирает одну строку из результирующего набора и помещает ее в ассоциативный массив, обычный массив или в оба
- mysqli_fetch_assoc() — Извлекает результирующий ряд в виде ассоциативного массива
- mysqli_fetch_object() — Возвращает текущую строку результирующего набора в виде объекта
- mysqli_query() — Выполняет запрос к базе данных
- mysqli_data_seek() — Перемещает указатель результата на выбранную строку
mysqli_fetch_row
result->fetch_row — Возвращает результат как численный массив.
mixed mysqli_fetch_row ( mysqli_result result )
Объектно-ориентированный стиль вызова (method):
class mysqli_result <
mixed fetch_row ( void )
>
Returns an array that corresponds to the fetched row, or NULL if there are no more rows.
mysqli_fetch_row() fetches one row of data from the result set represented by result and returns it as an enumerated array, where each column is stored in an array offset starting from 0 (zero). Each subsequent call to the mysqli_fetch_row() function will return the next row within the result set, or FALSE if there are no more rows.
Возвращаемые значения
mysqli_fetch_row() returns an array that corresponds to the fetched row or NULL if there are no more rows in result set.
Замечание: Эта функция устанавливает NULL-поля в значение NULL PHP.
Примеры
Пример 1. Объектно-ориентированный стиль вызова
$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); /* check connection */ if (mysqli_connect_errno()) < printf("Connect failed: %s\n", mysqli_connect_error()); exit(); >$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5"; if ($result = $mysqli->query($query)) < /* fetch object array */ while ($row = $result->fetch_row()) < printf ("%s (%s)
\n", $row[0], $row[1]); > /* free result set */ $result->close(); > /* close connection */ $mysqli->close();
Пример 2. Процедурный стиль вызова
$link = mysqli_connect("localhost", "my_user", "my_password", "world"); /* check connection */ if (mysqli_connect_errno()) < printf("Connect failed: %s\n", mysqli_connect_error()); exit(); >$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5"; if ($result = mysqli_query($link, $query)) < /* fetch associative array */ while ($row = mysqli_fetch_row($result)) < printf ("%s (%s)
\n", $row[0], $row[1]); > /* free result set */ mysqli_free_result($result); > /* close connection */ mysqli_close($link);
Результат выполнения данного примера:
Pueblo (USA) Arvada (USA) Cape Coral (USA) Green Bay (USA) Santa Clara (USA)