Friday, 16 March 2018

php - Call to a member function fetch_assoc() on non-object. mysql




I have the below code to check if a row exists then do something. However, I am getting




Call to a member function fetch_assoc()




I have found out that if I put $check->get_result(); before $check->store_result(); This error goes away but when I do that and echo $check->num_rows; I get empty result even though I have one row in the table. What am I doing wrong?




var_dump($check_result); gives me the below



0bject(mysqli_result)#6 (5) { 
["current_field"]=> int(0)
["field_count"]=> int(25)
["lengths"]=> NULL
["num_rows"]=> int(1)
["type"]=> int(0)
}



here is the query...



$material = '';

$check_added = $mysqli->prepare("select * from table where material = ? order by id desc limit 1");
$check_added->bind_param('s', $material);
$check_added->execute();
$check_result = $check_added->get_result();
$check_added->store_result();

echo $check_added->num_rows;
if($check_added->num_rows < 1)
{
//Do something
}
else
{
while($checkrow = $check_result->fetch_assoc()){
echo $checkrow['title'];
}


Answer



$check_added is a mysqli prepared statement object that you're treating like a result set, have you treid $check_result->num_rows?


No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...