2017-12-11 1 views
0

私はにデータベースからすべての私の結果を取得しようとしていますし、配列の指定されたmysqli_resultれることを想定しています PHPのmysqli_fetch_assocは、()は、パラメータ1は、物体が

私はとの結果をフェッチしようと

$sql = "SELECT * FROM posts WHERE user = ?"; 

if($stmt = mysqli_prepare($link, $sql)){ 
    // Bind variables to the prepared statement as parameters 
    mysqli_stmt_bind_param($stmt, "i", $param_username_int); 

    // Set parameters 
    $param_username_int = $user; 

    // Attempt to execute the prepared statement 
    if(mysqli_stmt_execute($stmt)){ 

     mysqli_stmt_store_result($stmt); 

     mysqli_fetch_assoc($stmt); 

    } 

    // Close statement 
    mysqli_stmt_close($stmt); 
} 
このライン:

mysqli_fetch_assoc($stmt); 

私は配列に結果を得ることを期待しています。

私はこのエラーを取得する:

mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, object given 

答えて

0

From the docs:

It is unnecessary to call mysqli_stmt_store_result() for other queries, but if you do, it will not harm or cause any notable performance loss in all cases.

あなたは本当にmysqli_stmt_store_resultを使用する必要がありますか?もしそうなら、あなたはそうのように、変数をバインドしてからmysqli_stmt_fetchを使用してデータを取得する必要があります。

$stmt->execute(); 
$stmt->bind_result($postTitle, $postContent); //or whatever your db fields are 

while ($stmt->fetch()) { 
    printf ("%s (%s)\n", $postTitle, $postContent); //loop through all returned rows and display the post title and content 
} 

ない場合は、mysqli_stmt_get_resultを使用することができますし、そのようにのように、結果にmysqli_fetch_assocを呼び出す:

mysqli_stmt_execute($stmt); 
$result = mysqli_stmt_get_result($stmt); //get result object 
while ($row = mysqli_fetch_assoc($result)){ //get associative array 

    /*Do whatever you want with result set here*/ 

} 

これが役立つことを願っています!

0
<?php 
$con=mysqli_connect("localhost","my_user","my_password","my_db"); 
// Check connection 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname"; 
$result=mysqli_query($con,$sql); 

// Associative array 
$row=mysqli_fetch_assoc($result); 
printf ("%s (%s)\n",$row["Lastname"],$row["Age"]); 

// Free result set 
mysqli_free_result($result); 

mysqli_close($con); 
?> 
関連する問題