无法在while循环php mysqli中执行update语句

时间:2014-01-25 23:36:11

标签: php mysql mysqli

我有以下查询

$products = $this->mysqliengine->query("select * from temp_all_product where download_status = 0") or die($this->mysqliengine->error());
    $temp_status_update = $this->mysqliengine->prepare("update temp_all_product set download_status = ? where id = ?") or die($this->mysqliengine->error);
    $temp_status_update->bind_result($download_status, $id);

    while($product = $products->fetch_assoc()) {
        $id = $product['id'];
        $download_status = 1;
        $temp_status_update->execute();
    }

在上面的语句中,我可以从temp table中选择值,但无法更新状态。这是什么问题

1 个答案:

答案 0 :(得分:0)

您需要在update语句中使用bind_param而不是bind_result。

$temp_status_update->bind_param('dd', $download_status, $id);

'dd'告诉系统每个输入都是一个数字。

http://www.php.net/manual/en/mysqli-stmt.bind-param.php

@eggyal只是建议你用一个更新语句替换所有代码。你对LIMIT的评论没有多大意义。

建议:如果你没有太多投资mysqli,那么切换到PDO。它允许使用命名参数,这些参数可以使您的代码更健壮,更易于维护:

$sql = "UPDATE temp_all_product SET download_status = :status where id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute(array('status' => 1, 'id' => $product['id']));

另外,您可以将其配置为抛出异常,因此您不需要进行所有此错误检查。

http://www.php.net/manual/en/book.pdo.php http://net.tutsplus.com/tutorials/php/pdo-vs-mysqli-which-should-you-use/