【问题标题】:A question about query result judgement in PHP and MySQL关于PHP和MySQL中查询结果判断的一个问题
【发布时间】:2010-02-25 06:17:20
【问题描述】:

代码如下:

$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
}

在问题行中,我希望条件找出是否已获取数据库中的某些记录。但是我发现它并不总是有效。有人建议我使用select count(*) as num ... 来做这个,但我认为这将是某种重复查询,如select * from...

【问题讨论】:

  • 我们不确切知道 Database 类中的内容,所以我认为我们不能真正提供帮助......
  • Databaseserver 类基于 PDO:)

标签: php mysql pdo


【解决方案1】:

这也应该有效,因为查询进入了数据库:

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

【讨论】:

  • @Justin Johnson:你是对的,最初我虽然那样但后来没有这样做:)
猜你喜欢
  • 1970-01-01
  • 2011-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-05
相关资源
最近更新 更多