开发者

A question about query result judgement in PHP and MySQL

开发者 https://www.devze.com 2022-12-21 00:42 出处:网络
Code as follows: $db = DatabaseService::getInstance();//get a database handle,base on pdo $sql = \"select * from authusers where ssouid = \'\".$ssouid.\"\' order by regtime\";

Code as follows:

$db = DatabaseService::getInstance();     //get a database handle,base on pdo
$sql = "select * from authusers where ssouid = '".$ssouid."' order by regtime";
$res = $db->query($sql);

if($res && $res->fetch())   // here is the Problem line
{
  //do something
}
else
{
  //raise some exception
}

In the problem line,I want the condition to find out whether some record(s) in database has been fetched or not.But I found somehow it didn't always work.Some开发者_StackOverflowone suggested me to use select count(*) as num ... to do this ,but I think that would be some kinda of duplicate query as select * from....


This should also work because query made its way to db:

if(mysql_num_rows($res))   // here is the Problem line
{
  //do something
}
else
{
  //raise some exception
}
0

精彩评论

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