PDO更新返回成功,但不更新数据库 [英] PDO update returns success, but doesn't update the database

查看:46
本文介绍了PDO更新返回成功,但不更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PDO更新表中的列.它运行正常,并且execute()返回1(成功).但是,当我检查数据库时,status列中的值没有更改.

I'm using PDO to update a column in a table. It runs fine, and execute() returns 1 (success). But, when I check the database the value in the status column hasn't changed.

/*
 * Add jobs to the beanstalkd queue
 */
foreach($jobs as $job) {

    $pheanstalk->useTube('scraper')->put(json_encode([
        'username' => $job['username'],
        'password' => $job['password'],
        'proxy'    => $job['proxy'],
        'gender'   => $job['gender'],
        'age'      => $job['age'],
        'device'   => $job['device']
    ]));

    /*
     * Update the row in the database and change its status to "queued"
     */
    try {
        $sql = "UPDATE cron_jobs SET status = :status WHERE id = :cid";
        $dbh->prepare($sql);
        $sth->bindValue(":status", "queued");
        $sth->bindValue(":cid", $job['cid']);
        print $sth->execute();
    } catch(PDOException $e) {
        syslog(LOG_ERR, $e->getTraceAsString());  
        exit;
    }    
}

有什么想法吗?它没有到达catch块,也没有异常发生.

Any ideas? It's not reaching the catch block, and no exceptions occur.

推荐答案

发现了错误.这个:

$dbh->prepare($sql);

应该是:

$sth = $dbh->prepare($sql);

我在上面的脚本中有另一个查询,我经常重用变量名.我完全忘记添加变量.总是愚蠢的错误会让您最长.

I had another query in the script above, I often reuse variable names. I totally forgot to add the variable. It's always the silly mistakes that catch you out the longest.

这篇关于PDO更新返回成功,但不更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆