不使用while循环获取mysql数据以降低速度?

发布于 2024-12-11 21:06:39 字数 376 浏览 0 评论 0原文

如果我只有一行要通过,

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仙女 2024-12-18 21:06:39

如果您只返回一行,则只获取一次。

if ($row = mysql_fetch_array($result))
{
  $comments = $row['comments'];
}

If you're only returning one row then only fetch once.

if ($row = mysql_fetch_array($result))
{
  $comments = $row['comments'];
}
清君侧 2024-12-18 21:06:39
$sql = mysql_query("
SELECT id,user,comments 
FROM user 
WHERE id='15' 
AND user = '15' 
LIMIT 1");

LIMIT 1 将在第一个结果后停止查询

$sql = mysql_query("
SELECT id,user,comments 
FROM user 
WHERE id='15' 
AND user = '15' 
LIMIT 1");

The LIMIT 1 will stop the query after the first result

一袭白衣梦中忆 2024-12-18 21:06:39

当结果集中有多于一行时,然后只需要在循环中执行此操作,否则要获取单行,则不需要将结果集放入循环中。

你可以这样做..

if(mysql_num_rows($result) == 1){
   if($row = mysql_fetch_array($result)){
       $comments = $row['comments']; 
   }
}

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..

if(mysql_num_rows($result) == 1){
   if($row = mysql_fetch_array($result)){
       $comments = $row['comments']; 
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文