<?php
$db = new mysqli('localhost', 'root', 'root', 'chatting');
$query = "SELECT * FROM user WHERE state = 1 AND getp = 0";
$result = $db->query($query);
$num_result = $result->num_rows;
$mems = "";
for ($i = 0; $i < $num_result; $i++) {
$row = $result->fetch_assoc();
$mems = $row["userName"] . " " . $mems;
$query = "update `user` set `getp` = 1 where 'userName` = ".' $row["userName"] ';
$result = $db->prepare($query);
}
echo $mems;
?>
我想要的是我想要获取state = 1和getp = 0的所有记录,然后在内部循环中并且对于每个记录我想将getp的值更改为1,这是正确的但不知道哪里出错了
答案 0 :(得分:2)
您忘记了execute()
更新声明..
另外,请注意,这不是准备语句的正确方法..您必须执行以下操作:
$query = $db->prepare("update `user` set `getp` = 1 where `userName` = :userName");
$query->bind_param(':userName',$row["userName"]);
$result = $query->execute();
答案 1 :(得分:-1)
为什么要在for循环中执行此操作?您可以UPDATE
用户SET getp = 1 WHERE state = 1 AND getp = 0;
用$ i ++循环时?为什么不while($row = $result->fetch_assoc()) { ... }
?
但是将查询更改为
$query = 'update `user` set `getp` = 1 where userName = "'. $row["userName"] .'"';
并且不要忘记执行()它。
答案 2 :(得分:-2)
请将您的更新命令设置为:
$query = "update `user` set `getp` = 1 where `userName` = '". $row["userName"]. "'";