Nukhba Arshad Nukhba Arshad - 3 months ago 5
PHP Question

Uncaught Error: Cannot use object of type mysqli_result as array

Fatal error: Uncaught Error: Cannot use object of type mysqli_result as array in C:\xampp\htdocs\tourist\AddFavorites.php:20
Stack trace:

0 {main}



thrown in
C:\xampp\htdocs\tourist\AddFavorites.php on line

$user_id = $_POST['user_id'];
$place_id = $_POST['place_id'];

$sth = ("select count(*) as count from favorites WHERE place_id=$place_id
AND user_id=$user_id");

$result = $con->query($sth);

if($result['count']>0){

$output = array(
'status'=>"0",
'operation'=>"already exists"
);

echo json_encode($output);
$db = null;

return;
}
else{
..}


and line number 20 is if($result['count']>0) I've looked for existing solutions but it's still not working.

Answer

After running the query

$result = $con->query($sth);

$result is not an Array, it is usually a Resource or TRUE or FALSE. You can check your result for error like this

if (!$result){
// This is an error
}

If you want to check for number of rows you can do it like this

if (mysql_num_rows ($result) > 0){
// No or rows greater than 0
}

I strongly suggest to check out the PHP manual

http://php.net/manual/en/function.mysql-query.php

Or even better use mysqli

http://php.net/manual/en/book.mysqli.php

Which you should really be using by now...