How to remove the fatal error when fetching an ass

2019-01-07 00:44发布

I am receiving a fatal error in my php/mysqli code which states that on line 46:

Fatal error: Call to undefined method mysqli_stmt::fetch_assoc() in ...

I just want to know how can I remove this fatal error?

The line of code it is pointing at is here:

$row = $stmt->fetch_assoc();

ORIGINAL CODE:

$query = "SELECT Username, Email FROM User WHERE User = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s",$user);
// execute query
$stmt->execute(); 
// get result and assign variables (prefix with db)
$stmt->bind_result($dbUser, $dbEmail);
//get number of rows
$stmt->store_result();
$numrows = $stmt->num_rows();                                      

if ($numrows == 1){

$row = $stmt->fetch_assoc();
$dbemail = $row['Email'];

}

UPDATED CODE:

$query = "SELECT Username, Email FROM User WHERE User = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s",$user);
// execute query
$stmt->execute(); 
// get result and assign variables (prefix with db)
$stmt->bind_result($dbUser, $dbEmail);
//get number of rows
$stmt->store_result();
$numrows = $stmt->num_rows();                                      

if ($numrows == 1){    
  $row = $stmt->fetch_assoc();
  $dbemail = $row['Email'];    
}

标签: php mysqli
5条回答
够拽才男人
2楼-- · 2019-01-07 01:09

The variable $stmt is of type mysqli_stmt, not mysqli_result. The mysqli_stmt class doesn't have a method "fetch_assoc()" defined for it.

You can get a mysqli_result object from your mysqli_stmt object by calling its get_result() method. For this you need the mysqlInd driver installed!

$result = $stmt->get_result();
row = $result->fetch_assoc();

If you don't have the driver installed you can fetch your results like this:

$stmt->bind_result($dbUser, $dbEmail);
while ($stmt->fetch()) {
    printf("%s %s\n", $dbUser, $dbEmail);
}

So your code should become:

$query = "SELECT Username, Email FROM User WHERE User = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s",$user);
// execute query
$stmt->execute(); 
// bind variables to result
$stmt->bind_result($dbUser, $dbEmail);
//fetch the first result row, this pumps the result values in the bound variables
if($stmt->fetch()){
    echo 'result is ' . dbEmail;
}
查看更多
在下西门庆
3楼-- · 2019-01-07 01:10

It's best to use mysqlnd as Asciiom pointed out. But if you're in a weird situation where you are not allowed to install mysqlnd, it is still possible to get your data into an associative array without it. Try using the code in this answer Mysqli - Bind results to an Array

查看更多
相关推荐>>
4楼-- · 2019-01-07 01:13

You have missed this step

$stmt = $mysqli->prepare("SELECT id, label FROM test WHERE id = 1");
$stmt->execute();
$res = $stmt->get_result(); // you have missed this step
$row = $res->fetch_assoc();
查看更多
你好瞎i
5楼-- · 2019-01-07 01:20

I realized that this code was provided as an answer somewhere on stackoverflow:

//get number of rows
$stmt->store_result();
$numrows = $stmt->num_rows();

I tried it to get the number of rows but realized that i didnt need the line $stmt->store_result();, and it didn't get me my number. I used this:

$result      = $stmt->get_result();
$num_of_rows = $result->num_rows;
......
$row         = $result->fetch_assoc();
$sample      = $row['sample'];
查看更多
手持菜刀,她持情操
6楼-- · 2019-01-07 01:28

Change,

$stmt->store_result();

to

$result = $stmt->store_result();

And

Change,

$row = $stmt->fetch_assoc();

to

$row = $result->fetch_assoc();
查看更多
登录 后发表回答