php mysqli check if mysqli query returns false

mysqli select query
mysqli if result is empty
mysqli_query not working
php mysqli->query result
php mysql query result
mysqli_error
mysqli oop query
mysqli procedural query

How can I check mysqli query if it returns boolean(false) or the result? If I try getting the num_rows I get php error because I'm trying to access a non object as object. But I need to check this because if its false I have to set a variable and if its not than get the result of the query.

my query looks like this:

<?php
$q = "SELECT `id` FROM `table` ORDER BY `id` DESC LIMIT 0, 1";
$res = mysqli->query($q);
?>

You need to use === operator which also checks arguments type:

$q = "select ,....";
$res = mysqli->query( $q );

if( $res !== false ) { 
   // query ok
} else {
   // query failed
}

mysqli::query - Manual, (PHP 5, PHP 7). mysqli::query -- mysqli_query — Performs a query on the database Returns FALSE on failure. For successful SELECT check connection */ if  Return Values. Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

To know if a variable is set to false, you can use

if($res === false){//strictly false, no 0 or ''
   //do something
}

In this case you can just say you want to display the error in order to correct it :

$res = mysqli->query($q) or exit mysqli_error();

Efficient way to check if a mysql query returned no results?, Efficient way to check if a mysql query returned no results? Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE. This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include: mysql_query () sends a unique query (multiple queries are not supported) to the currently

if (!$mysqli->query("SET a=1")) {
    printf("Errormessage: %s\n", $mysqli->error);
}

Query returning results when database table is empty - PHP, I've stopped and restarted my apache and mysql in XAMPP. So, I did I know it isn't the database table because I changed the query to query another/different table and it returned a result = 1 Returns FALSE on failure. For  MySQL Database MySQL Connect MySQL Create DB MySQL Create Table MySQL Insert Data MySQL Get Last ID MySQL Insert Multiple MySQL Prepared MySQL Select Data MySQL Delete Data MySQL Update Data MySQL Limit Data.

PHP mysqli query() Function, PHP MySQLi Reference Check connection if The query() / mysqli_query() function performs a query against a database. Return Value: For successful SELECT, SHOW, DESCRIBE, or EXPLAIN queries it will return a FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

Performs a query on the database, Example #1 mysqli::query() example. Object oriented style. <?php $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); /* check connection */ if  Return value: Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE. Version: PHP 5, PHP 7. Example of object oriented style:

MySQL PHP API :: 3.9.37 mysqli::query, mysqli_query, Returns FALSE on failure. For For other successful queries mysqli_query will return TRUE . check connection */ if ($mysqli->connect_errno) { printf("Connect failed: %s\n", $mysqli->connect_error); exit(); } /* Create table doesn't return a  Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent Database Connections Safe Mode Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation

Comments
  • if error occurs, can I after this, call mysqli_close?
  • by default, as mysqli is OOP compatible, the connection is destroyed automaticaly.