不使用while循环获取mysql数据以降低速度?
如果我只有一行要通过,
$sql = mysql_query("SELECT id,user,comments FROM user WHERE id='15' AND user = '15'");
有没有一种方法可以从该行获取注释,而无需经过 while 循环:
while($row = mysql_fetch_array($sql)){
$comments = $row['comments'];
}
除了使用 while 循环之外,还有类似的东西吗?如果我的数据有几千到几百万条数据,一个while循环会花很长时间?这是定位某一行并降低其速度的更合乎逻辑的方法吗?
感谢您的帮助!
If I only have one row that I'm getting through
$sql = mysql_query("SELECT id,user,comments FROM user WHERE id='15' AND user = '15'");
is there a way to just get the comments from that one row without going through a while loop :
while($row = mysql_fetch_array($sql)){
$comments = $row['comments'];
}
Is there something similar besides using a while loop? If my data has thousands to millions of data, a while loop will take a long time? Is this a more logical way to locate a certain row and reduce its speed?
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只返回一行,则只获取一次。
If you're only returning one row then only fetch once.
LIMIT 1 将在第一个结果后停止查询
The LIMIT 1 will stop the query after the first result
当结果集中有多于一行时,然后只需要在循环中执行此操作,否则要获取单行,则不需要将结果集放入循环中。
你可以这样做..
when you have more than one rows in resultset, then and then only you need to do it in loop else to fetch single row you are not required to put result set in loop.
you can do it like this..