Do a sample

$result = $mysqli->query("SELECT time FROM LeadsTable WHERE phone = $phone LIMIT 1"); 

How to determine if there is an entry in the database?

  • if ($result) means returned what was requested - Alexey Shimansky
  • I immediately used this option, but for some reason it does not work. Therefore, I thought that I needed something different. - Frontender
  • I always have true - Frontender
  • Here you need to look at what you have under the word не работает ..... and of course the code together, not divorced from ...... - Alex Shimansky
  • This is just the whole code :-) Only connection to the database is higher. - Frontender

1 answer 1

If $mysqli is an object of the native class mysqli , then the query method for a select query will always return a result object if the query was executed without errors. No rows found - this is a special case of successful execution. And casting an object to a boolean type is always true . Therefore, if ($result) , of course, will only say that the request has been completed.

You need to check or the number of lines in the result:

 if ($result->num_rows > 0) 

Or call any of the fetch_ methods to get the result string. If fetch says NULL , then no rows were found.


Note the possible SQL injection. In general, never substitute data in the query text and use prepared statements . Then you will not have to worry about this question, and the use of a variable in the query text should cause anxiety and a desire to check what the variable is and whether it should be inserted as is.