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().
mysqli_result::fetch_row
mysqli_fetch_row
(PHP 5)
mysqli_result::fetch_row -- mysqli_fetch_row — 結果の行を数値添字配列で取得する
説明
オブジェクト指向型
手続き型
結果セットからデータを 1 行取得し、それを数値添字の配列で
返します。各カラムは 0(ゼロ)から始まる添字に格納されます。
mysqli_fetch_row() 関数を続けてコールすると、
結果セットの次の行を返します。もしもう行がない場合には
NULL を返します。
返り値
mysqli_fetch_row() は取得した行に対応する文字列の配列を
返します。もしもう行がない場合には NULL を返します。
注意: この関数は、 NULL フィールドに PHPの
NULL値を設定します。
例
例1 オブジェクト指向型
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* 接続状況をチェックします */
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)) {
/* 配列を取得します */
while ($row = $result->fetch_row()) {
printf ("%s (%s)\n", $row[0], $row[1]);
}
/* 結果セットを開放します */
$result->close();
}
/* 接続を閉じます */
$mysqli->close();
?>
例2 手続き型
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* 接続状況をチェックします */
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)) {
/* 配列を取得します */
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() - 結果の任意の行にポインタを移動する
evangun2001 at yahoo dot fr
16-Sep-2007 11:24
Stephen
23-Jan-2007 11:29
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.
maillist at pnpitalia.it
06-Jan-2004 07:01
from "README.PHP4-TO-PHP5-THIN-CHANGES"
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()
