开发者

PHP if a table table row exists

In PHP, is there a way of making the following statement to check if a row exists?

If "the table cases has a "passenger" field with the value of 4 WHERE case= '$case'开发者_如何转开发 { code... }


$query ='SELECT 1 FROM cases WHERE passenger = 4 AND case ="$case"';
$result = mysql_query($query);

if(mysql_num_rows($result)){
   //exists
}


not that elegantly, no. But you can run the query then compare the result:

$case = mysql_real_escape_string($case); // make sure to escape values going in queries
if (($r = mysql_query("SELECT * FROM cases WHERE passenger=4 AND case='{$case}'")) !== false)
{
  if (mysql_num_rows($r))
  {
     // your code here.
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜