How to check the existence of a record in mysql without returning the result set? There is a request, for example:

mysqli_query("SELECT id FROM table WHERE id = 1;") 

Such a query will return the result set, and is it possible to somehow simply check the existence of a string?

  • so this will be the check. If I returned the record, it means it is ... if it returns anything, it means there is no record ... Or did I not understand the question? - cyadvert
  • A server with such a request will return a string with the result, and is it possible to do something like to return either true or false? - Hit-or-miss
  • one
    mysqli_query("SELECT count(id)>0 FROM table WHERE id = 1;") will answer True or False - Saidolim
  • And if I put the question differently. How correctly from the point of view of mysql to check the existence of a string? And is it necessary at the end of limit 1 if the comparison is carried out by a unique index? - Hit-or-miss
  • maybe someone can explain to me what is the fundamental difference? the request returns something, or is true/false ? I don’t mind ... just want to understand ... maybe then I’ll make checks differently ... - cyadvert

2 answers 2

There are many options to check. Choose one who likes what


Using the EXISTS command

 SELECT EXISTS(SELECT id FROM table WHERE id = 1) 

documentation here


Use number of values

 $res = mysql_query('select count(*) FROM table WHERE id = 1') or die(); $row = mysql_fetch_row($res); if ($row[0] > 0) { // Есть данные } else { // нет данных } 

mysql_num_rows operator

 $res = mysql_query("SELECT id FROM table WHERE id = 1"); $count = mysql_num_rows($res); if( $count > 0 ) { // Есть данные } else { // нет данных } 
  • 3
    and what simply select 1 from table where id = 1 limit 1 did not please? if id is not a key, you can spend a lot of time checking - BOPOH
  • @BOPOH agree, also a good option - Saidolim
  • What does odinitsa after select mean? - Hit-or-miss
  • Saidolin, thanks for the player I read now - Hit-or-miss
  • 2
    @Panilipropal, 1 is a constant so as not to drag real data from the database when we don’t need it (especially when data from different tables is selected). In your particular case, this will be superfluous (since id is a key and it is unique), but the answers are not only for you, but also for other visitors, and their requests can be very complex. - BOPOH
 mysqli_query("SELECT Count(id) FROM table WHERE id = 1;") 

This query will return 1 if there is a string and 0 if it does not, provided that id is unique

  • one
    Why drive mysql data back and forth? if you need to check, do so: select 1 from table where id = 1 limit 1 , now this example can be applied to almost any query. And in your case, if the search is not on a key basis, the query can take a very long time to complete - BOPOH
  • @BOPOH why such a search is needed then if Id not the key? Uniqueness check? Well, then she is unique in order to issue an eXception - Dmitry
  • Uniqueness is also a key, “why do you need” - getting the result set can take a very long time, even if nothing is returned in the end. In order not to perform such heavy queries once again, you can run its light version and check that the data really is. And why find all records with id = 1, if we are only interested in the presence of at least one such record? But the count is just looking for them all. By removing count and adding limit, we can save a lot of time. And choosing from table 1 instead of real data, we simply save on transferring data with a heap of joins - BOPOH
  • In my case, the key is unique - Hit-or-miss
  • @BOPOH agree - Dmitry