check if SQL row exists with PHP

后端 未结 3 811
时光取名叫无心
时光取名叫无心 2020-12-05 15:25

I\'m using MySQL with PHP and I need to do something like this (pseudocode):

if (sql row exists where username=\'bob\')
{
    // do this stuff
}
3条回答
  •  温柔的废话
    2020-12-05 16:06

    another approach

    $user = "bob";
    $user = mysql_real_escape_string($user);
    $result = mysql_query("SELECT COUNT(*) AS num_rows FROM my_table WHERE username='{$user}' LIMIT 1;");
    $row = mysql_fetch_array($result);
    if($row["num_rows"] > 0){
       //user exists
    }
    

提交回复
热议问题