1

我似乎无法让这段代码工作。您可以使用 foreach 行运行另一个查询吗?这是一个将发送电子邮件的 cron 脚本,我需要在 cron 运行时将状态从“0”更新为“1”,以便我知道电子邮件已发送 -下面的代码 UPDATE 查询不起作用。

// Connect to the database
$conn = new PDO("mysql:host=$DB_HOST;dbname=$DB_DATABASE",$DB_USER,$DB_PASSWORD);

// Get the emails to send and ensure the status shows they have not been sent already
$stmt = $conn->prepare("SELECT id, userid, title, message FROM reminders WHERE time=CURDATE() AND status='0'");
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach($results AS $row){
echo $row['title'] . ' ' . $row['message'] . ' ' . $row['id'] . '<br />';
$update = $conn->prepare("UPDATE results SET status='1' WHERE id=:id");
$update->bindParam(':id', $row['id']);
$update->execute();
};

编辑:表名错误。解决了

4

3 回答 3

4

如何指定值的数据类型(我认为[不确定]默认数据类型是字符串,服务器会自动解析它)?PDO::PARAM_INT

$update->bindValue(':id', $row['id'], PDO::PARAM_INT);

PDO 预定义常量

于 2012-09-07T06:28:03.190 回答
2

也许您需要bindValue并指定数据类型:

$update = $conn->prepare("UPDATE results SET status=1 WHERE id=:id");
$update->bindValue(':id', $row['id'], PDO::PARAM_INT);
$update->execute();
于 2012-09-07T06:26:54.080 回答
0

只是为了在问题 - 答案格式中有正确的解决方案:

You are using the result variable resuls and then try to update the table results. Is the table you want to update really named "results" or has this information creeped in from the name of the variable?

From the query it look like you are working on the table reminders.

于 2012-09-07T06:44:52.253 回答