关于PHP和MySQL查询结果判断的问题
代码如下:
$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...
。
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.Someone 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...
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这也应该有效,因为查询已到达数据库:
This should also work because query made its way to db: