简体   繁体   English

如何在php中使用所有mysql查询结果

[英]how to use all mysql query results in php

i need to get and update all results of the query, i get all the results but it updates only the first result, why? 我需要获取并更新查询的所有结果,我得到所有结果,但它只更新第一个结果,为什么? Thanks in advance. 提前致谢。

$result=mysql_query(" SELECT text FROM sess WHERE dest='$name' AND mitt='$mitt' AND read='0'  ORDER BY idm  ");
 while($row = mysql_fetch_array($result)){
         $messages[]=$row[$i];
         $mess= mysql_real_escape_string($row[$i]);
         mysql_query(" UPDATE sess SET read='1' WHERE dest='$name' AND mitt='$mitt' AND text='$mess' ");
         $i++;
        }

You need to access the first index of your result row. 您需要访问结果行的第一个索引。

$result=mysql_query(" SELECT text FROM sess WHERE dest='$name' AND mitt='$mitt' AND read='0'  ORDER BY idm  ");
while($row = mysql_fetch_array($result)){
     $messages[]=$row[0];
     $mess= mysql_real_escape_string($row[0]);
     mysql_query(" UPDATE sess SET read='1' WHERE dest='$name' AND mitt='$mitt' AND text='$mess' ");
     $i++;
}

Note: that the functions you are using mysql_* are deprecated. 注意:不推荐使用mysql_*的函数。 There are some alternatives, mysqli_* being a direct replacement. 有一些替代品, mysqli_*是直接替代品。 Read more here 在这里阅读更多

Note 2: Use the ID of the message to set the message as read. 注意2:使用消息的ID将消息设置为已读。 Using the content of the text is inefficient. 使用文本内容效率低下。

UPDATE sess SET read='1' WHERE message_id = $messageId

为什么不这样做呢

UPDATE sess SET read='1' WHERE dest='$name' AND mitt='$mitt' AND read='0' ?

try that : 试试看:

  while($row = mysql_fetch_array($result)){

     mysql_query(" UPDATE sess SET read='1' WHERE dest='$name' AND mitt='$mitt' AND text='".$row['text']."' ");

    }

or this: 或这个:

 while($row = mysql_fetch_assoc($result)){
 $id = mysql_real_escape_string($row['id']);
 mysql_query("UPDATE sess SET read='1' WHERE `id` = '$id' ");
}

Try this 尝试这个

$result=mysql_query("SELECT id ,text FROM sess WHERE dest='$name' AND mitt='$mitt' AND read='0'  ORDER BY idm  ");
while($row = mysql_fetch_assoc($result)){
     $messages[]=$row['text'];
     $id = mysql_real_escape_string($row['id']);
     mysql_query("UPDATE sess SET read='1' WHERE `id` = '$id' ");
     $i++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM