Return row count from mysqli prepared statement
You should definitely not be mixing procedural and object-oriented syntax.
Although it works with un-mixed syntax, the process is working harder than it needs to and delivering more result set data than you intend to use.
I would add COUNT(1) or COUNT(*) to the sql like this:
$sql = &quot;SELECT COUNT(1) FROM notifications WHERE viewed='0' AND user_to = ?&quot;;
$query = $this-&gt;con-&gt;prepare($sql);
$query-&gt;bind_param(&quot;s&quot;, $userLoggedIn);
$query-&gt;execute();
$query-&gt;bind_result($count);
$query-&gt;fetch();
return $count;

Assuming the sql is not broken due to syntax errors, this will always return a number.