2017-09-01 15 views
0
$id = $_REQUEST['id']; 

$stmt = $conn->prepare('SELECT * FROM test WHERE id = ?'); 
echo $id; 
$stmt->bind_param('s', $id); 

$stmt->execute(); 

// This is line 12 
while ($row = $stmt->fetch()) { 
    $test = $row['test']; 
} 

これでわかりました。私は基本的に私は私が間違っているのかわからないが、私はまだあまり機能していないbind_result()$stmt->fetch_assoc()を試してみました準備されたステートメントがデータをフェッチしない

#0 /example/example.php(2): require() 
#1 {main} 
    thrown in /example/example.inc.php on line 12 

次私に語ったエラーが出る原因このコードは、動作しません。私はここに他の多くの質問を読んだが、彼らは私を助けなかった。ここで

はあなたが右のプリペアドステートメントをやっていない接続

<? 
$servername = "exase"; 
$username = "exaus"; 
$password = "exapw"; 
$dbname = "exa_db"; 

$conn = new mysqli($servername, $username, $password, $dbname); 

if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

$conn = new mysqli($servername, $username, $password, $dbname); 

if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 
+0

あなたの接続はどのようなものが見えますか? –

+1

実際のエラーメッセージとはなんですか? (そのスタックトレースの上の行) – rickdenhaan

+0

paramを文字列ではなくintとしてバインドする必要があります。 '$ stmt-> bind_param( 'i'、$ id);あなたの問題を解決するかどうかは分かりませんが、現時点では正しく実行されていません。 – GrumpyCrouton

答えて

0

を。結果を取得するときにデータを使用するには、mysqli_stmt::bind_result()を使用するか、結果セットをmysqli_stmt::get_result()で保存する必要があります。 get_result()を使用するには、MySQLネイティブドライバmysqlndが必要です。そうしないと、手動で各カラムをbind_result()にバインドする必要があります。

ここでは、bind_result()の使用例を示します。 SELECT *を実行するので、テーブルにあるすべてのもの(、ただし)をバインドする必要があるため、後でテーブルにカラムを追加するとそのメソッドは失敗します。そのため、必要な列だけを選択することをお勧めします(SELECT id, test FROM..など)。あなたはしかし、MySQLのネイティブドライバがインストールされている場合は

$id = $_REQUEST['id']; 

$stmt = $conn->prepare('SELECT test, id FROM test WHERE id = ?'); 
$stmt->bind_param('s', $id); 

$stmt->execute(); 
$stmt->bind_result($test, $result_id); 
$stmt->fetch()); 

/* 
* If there was any matching rows, the variables defined in bind_param() 
* now hold the values 
* You can then use '$test' and '$result_id' 
*/ 
echo $test; 

$stmt->close(); 

、あなたはget_result()を使用し、「通常の」クエリとしてそれを使用することができます。 SELECT *を実行してもそれほど重要ではありません(すべてを選択することはお勧めしませんが、必要な列は選択する必要があります)。

$id = $_REQUEST['id']; 

$stmt = $conn->prepare('SELECT * FROM test WHERE id = ?'); 
$stmt->bind_param('s', $id); 

$stmt->execute(); 
$result = $stmt->get_result(); // $result is now an object of MySQLi resource, and not MySQLi statement 
           // It can now be used as as the result-set of a regular query 

$row = $result->fetch_assoc()); 
$test = $row['test']; 
echo $test; 

$stmt->close(); 
+0

私が得意でないことは、なぜbind_resultで結果を得ているのですか? – onebooredguy

+0

これは 'fetch()'が呼び出されるたびに結果をバインドするだけなので(複数の行がある場合)あなたがそれを取得するまで、値は含まれません。そのIDと一致する行が1つしかない場合、 'while'を削除できますが、' bind_result() 'で定義された変数がデータベースの値を保持する前に' fetch() 'する必要があります。 – Qirel

+0

idカラムにはプライマリキーがありますので一意ですが、どのように取得できますか? – onebooredguy

0

あり、この参照:MySQLiをプリペアドステートメントを使用する場合は、標準のクエリを使用するよりも少し違う

$id = $_REQUEST['id']; 

    $stmt = $conn->prepare('SELECT col1,col2,col3 FROM test WHERE id = ?'); 
    $stmt->bind_param('i', $id); 
    $stmt->execute(); 
    $stmt->bind_result($col1,$col2,$col3); // you need to bind the result when using prepared statements 
    $stmt->store_result(); // store it so if the number of rows retrieved is large they won't be dropped during loop or cause the overload error 


    while ($stmt->fetch()) { 
     $test = $col1; 
     echo $col2; 
    } 

$stmt->close(); 
+0

すべての列が必要です – onebooredguy

+0

はい、追加してください。選択しないでください。* – clearshot66

+0

*を選択する際の問題点は何ですか? – GrumpyCrouton

関連する問題