SQL UPDATE语句不起作用

时间:2017-06-11 04:03:26

标签: php mysql sql

我是SQL的新手,我需要一些帮助。我试图设置发送= 0的所有地方,使它们全部发送= 1

这是我的代码:

<?php
$hostname = "localhost";
$username = "username";
$password = "password";
$db = "HomeMessages";

$mysqli = new mysqli($hostname,$username, $password, $db);  
$result = mysqli_query($mysqli, "SELECT * FROM notification_queue WHERE sent='0'");


if(mysqli_num_rows($result) > 0) {
     //email
     echo 'yes';
     $sql = "UPDATE notification_queue SET sent = '1' WHERE sent ='0'";
} else {
    //don't email
}
?>

它在页面上回显,所以我知道它正在尝试运行sql命令。

如果这有帮助,这是我的数据库:

database

2 个答案:

答案 0 :(得分:1)

您需要执行查询:

<?php
$hostname = "localhost";
$username = "username";
$password = "password";
$db = "HomeMessages";

$mysqli = new mysqli($hostname,$username, $password, $db);  
$result = mysqli_query($mysqli, "SELECT * FROM notification_queue WHERE sent='0'");


if(mysqli_num_rows($result) > 0) {
    //email
    echo 'yes';
    $result = mysqli_query($mysqli, "UPDATE notification_queue SET sent = '1' WHERE sent ='0'");
} else {
    //don't email
}
?>

答案 1 :(得分:0)

您的UPDATE查询从未运行,因为您从未调用mysqli_query()

更重要的是,我同意草莓。我认为你不需要SELECT查询。您可以查看&gt;的更新结果受影响的0行以查明是否有任何sent=0行。始终尝试尽可能少地运行查询。此外,在使用mysqli函数时选择面向对象或程序样式;任何一个,只需选择一个。这是面向对象的:

$hostname = "localhost";
$username = "username";
$password = "password";
$db = "HomeMessages";

$mysqli = new mysqli($hostname,$username, $password, $db);
$mysqli->query("UPDATE notification_queue SET sent=1 WHERE sent=0");
if($mysqli->affected_rows){
    //email
    echo 'yes';
} else {
    //don't email
}