【问题标题】:pdo binding returns false in updatepdo 绑定在更新中返回 false
【发布时间】:2013-02-20 22:22:35
【问题描述】:

我的更新脚本有一些问题。我绑定了我的值,但它返回 false,我看不到我做错了什么。

我正在运行这个:

$row = $db->query('
            UPDATE '. $config->db_prefix .'_adverts
            SET ad_type = ?, 
            title = ?, 
            text = ?, 
            price = ?, 
            category = ?, 
            condition = ?
            WHERE aid = ?')
                ->bind(1, $ad_type)
                ->bind(2, $title)
                ->bind(3, $text)
                ->bind(4, $price)
                ->bind(5, $category)
                ->bind(6, $condition)
                ->bind(7, $aid)->execute();
        }

绑定函数是这样的:

public function bind($pos, $value, $type = null) {

        if( is_null($type) ) {

            switch( true ) {
                case is_int($value):
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value):
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    break;
                default:
                    $type = PDO::PARAM_STR;
            }
        }

        $this->stmt->bindValue($pos, $value, $type);
        return $this;
    }

var_dump($this) 给了我:

object(DB)#1 (2) { ["dbh":protected]=> object(PDO)#2 (0) { } ["stmt":protected]=> object(PDOStatement)#15 (1) { ["queryString"]=> string(211) " UPDATE rno_adverts SET ad_type = ?, title = ?, text = ?, price = ?, category = ?, condition = ? WHERE aid = ?" } }

但我看不出有什么问题。

编辑:

查询函数是这样的:

public function query($query) {
        $this->stmt = $this->dbh->prepare($query);
        return $this;
    }

然后执行是这样的:

public function execute($var = null) {
        return $this->stmt->execute($var);
    }

错误:

Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'condition = 3 WHERE aid = 1'

查询的输出:

UPDATE rno_adverts SET ad_type = 3, title = "Gul bil", text = "En flot gul bil med hvide striber", price = 500, category = 4, condition = 3 WHERE aid = 1

我对这个查询视而不见,所以我看不出问题出在哪里。如果我删除类别和条件,它可以正常工作。这两个字段在数据库中都是 INT NOT NULL。

【问题讨论】:

  • $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  • 如果PDOStatement::execute() 返回FALSE,您不应断定您做错了什么,因为它是该函数的有效返回值。它只是意味着语句出现故障,并且语句本身包含错误的信息。只需选择错误信息php.net/manual/en/pdostatement.errorinfo.php 它会告诉您有关失败的更多信息。因此,您应该提供具体的错误信息,而不是只在您的问题中讲述 FALSE(这可能意味着很多)。
  • 应该注意上面的 pdo 脚本不是我的工作。在 SO 上找到它 - 找不到线程。问题更新错误
  • 这组函数在我看来完全没用。为什么不使用原始 PDO?
  • @YourCommonSense - 所以你说我不需要使用这组函数,而是使用 f.x 中的 PDO。我的单个广告更新函数( update_advert() )?

标签: php mysql pdo sql-update


【解决方案1】:

您只能将绑定函数与准备好的语句一起使用。 ->query() 用于生成查询,您事先已经知道查询的所有值/变量。

【讨论】:

  • 他仍在使用PDO::query 和占位符。对我来说没有意义。
  • 可能不命名prepare 方法query 的一个原因。我猜 OP 试图创建一个流畅的界面。
  • 他没有使用PDO::query(),返回语句的是他自己的代码。
【解决方案2】:

根据您的问题,无法具体说明错误所在的位置。因此,我建议您首先添加更多错误检查,例如使用bindvalue(),您无需检查返回值:

$this->stmt->bindValue($pos, $value, $type);

如果失败则抛出异常:

$bind = $this->stmt->bindValue($pos, $value, $type);
if (!$bind) {
    throw new Exception(sprintf("Unable to bind parameter %s with a value of type #%d", $pos, $type));
}

这将防止您在绑定值时遇到问题,但它被忽视了。

与执行类似。如果你想提供失败原因的信息,你需要读出错误信息并抛出异常。

【讨论】:

    【解决方案3】:

    只要去掉这个绑定函数就可以了

    $sql = 'UPDATE '. $config->db_prefix .'_adverts SET 
            ad_type = ?, title = ?, text = ?, price = ?, category = ?, condition = ?
            WHERE aid = ?';
    $db->query($sql);
    $db->execute(array($ad_type, $title, $text, $price, $category, $condition, $aid));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-29
      • 2015-05-18
      • 2017-12-07
      • 2015-01-28
      相关资源
      最近更新 更多