0

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'];
    }
Passerby
  • 9,715
  • 2
  • 33
  • 50
user3006683
  • 783
  • 2
  • 9
  • 16

1 Answers1

0

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

Zarathuztra
  • 3,215
  • 1
  • 20
  • 34