So what IS the best way to check if a row exists? EXISTS, COUNT or num_rows?

前端 未结 3 1136
情深已故
情深已故 2021-02-15 04:05

If your only goal is to check if a row exists in php (true or false), what is the best way to do it?

Option 1?

$result          


        
3条回答
  •  日久生厌
    2021-02-15 04:26

    Option 3 is the fastest way to check if a row exists if you are using MySQL:

    $query = mysql_query("SELECT EXISTS(SELECT 1 FROM users WHERE id = 1)")
    
    if (mysql_result($query, 0) == 1)
        // one user, like it should be.
    
    else 
      // do something else
    

提交回复
热议问题