mysqli_stmt
PHP Manual

mysqli_stmt::fetch

mysqli_stmt_fetch

(PHP 5, PHP 7)

mysqli_stmt::fetch -- mysqli_stmt_fetch — プリペアドステートメントから結果を取得し、バインド変数に格納する

説明

オブジェクト指向型

bool mysqli_stmt::fetch ( void )

手続き型

bool mysqli_stmt_fetch ( mysqli_stmt $stmt )

プリペアドステートメントから結果を読み込み、 mysqli_stmt_bind_result() でバインドした変数に格納します。

注意:

mysqli_stmt_fetch() をコールする前に、すべての カラムがバインド済みである必要があることに注意しましょう。

注意:

データの転送はバッファを用いずに行います。 mysqli_stmt_store_result() をコールするとバッファを使用し、パフォーマンスが減少します (しかしメモリのコストは下がります)。

パラメータ

stmt

手続き型のみ: mysqli_stmt_init() が返すステートメント ID。

返り値

返り値
値 説明
TRUE 成功。データが取得されました。
FALSE エラーが発生しました。
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 150,5";

if (
$stmt = $mysqli->prepare($query)) {

    
/* ステートメントを実行します */
    
$stmt->execute();

    
/* 結果変数をバインドします */
    
$stmt->bind_result($name, $code);

    
/* 値を取得します */
    
while ($stmt->fetch()) {
        
printf ("%s (%s)\n", $name, $code);
    }

    
/* ステートメントを閉じます */
    
$stmt->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 150,5";

if (
$stmt = mysqli_prepare($link, $query)) {

    
/* ステートメントを実行します */
    
mysqli_stmt_execute($stmt);

    
/* 結果変数をバインドします */
    
mysqli_stmt_bind_result($stmt, $name, $code);

    
/* 値を取得します */
    
while (mysqli_stmt_fetch($stmt)) {
        
printf ("%s (%s)\n", $name, $code);
    }

    
/* ステートメントを閉じます */
    
mysqli_stmt_close($stmt);
}

/* 接続を閉じます */
mysqli_close($link);
?>

上の例の出力は以下となります。

Rockford (USA)
Tallahassee (USA)
Salinas (USA)
Santa Clarita (USA)
Springfield (USA)

参考


mysqli_stmt
PHP Manual