【问题标题】:PHP Binding PDO arraysPHP 绑定 PDO 数组
【发布时间】:2012-04-27 07:22:45
【问题描述】:

我正在尝试通过一个数组使用 PDO 组装一个数据库插入,但只是在某处丢失了它,我正在寻找一些关于我所缺少的东西的帮助。该数组是一个关联数组。抛出的错误是:

Fatal error:  Uncaught exception \'PDOException\' with message \'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens\' in /var/www/html/themonastery.org/mot/receiver.php:70
Stack trace:
#0 /var/www/html/themonastery.org/mot/receiver.php(70): PDOStatement->execute()
#1 {main}
  thrown in /var/www/html/themonastery.org/mot/receiver.php on line 70

我使用的代码是:

    /** PDO Stuff **/

    //require and instantiate pdo instance
    require_once "dependancies/pdo.func.php";
    $dbh = pdo_connect();

    //implode query
    $keys = implode(',', array_keys($clean));
    $vals = implode(',', array_fill(0, count($clean), '?'));
    
    $insert = array_values($clean);

    //pdo prepare
    $sth = $dbh->prepare("INSERT INTO backupDB ($keys) VALUES ($vals)");

    //set loop condition
    $waiting = true; 
    while($waiting) {
        try {
        $dbh->beginTransaction();

        $i=1;
        foreach($clean as $insert) {

            // bindvalue is 1-indexed, so $k+1
            $sth->bindValue($i++, $insert, PDO::PARAM_STR);

            $sth->execute();
            sleep(1);
        }
        
        $dbh->commit();
        $waiting = false;

        } catch(PDOException $e) {
        if(stripos($e->getMessage(), 'DATABASE IS LOCKED') !== false) {
            //sleep for 0.25 seconds and try again.
            $dbh->commit();
            usleep(250000);
        } else {
            $dbh->rollBack();
            throw $e;
        }
        }
    }

这是关联数组,

array (
  'full_name' => 'First Middle Last Suffix',
  'first_name' => 'First',
  'middle_name' => 'Middle',
  'last_name' => 'Last Suffix',
  'address' => 'The Address',
  'city' => 'City',
  'state' => 'State Abbr',
  'zip' => 'Zip code',
  'country' => 'Country Abbr',
  'email' => 'dev@null.com',
  'password' => 'd41d8cd98f00b204e9800998ecf8427e',
  'ordinationDate' => '2012-04-15',
  'birthday' => '1982-14-01',
  'isValidAge' => '1',
)

根据要求,这里是 $keys$vals 的 var_dump

$keys = string(123) "full_name,first_name,middle_name,last_name,address,city,state,zip,country,email,password,ordinationDate,birthday,isValidAge"
$vals = string(27) "?,?,?,?,?,?,?,?,?,?,?,?,?,?"

这是来自数据库的列名

    id  full_name   first_name  middle_name last_name   address city    state   zip country email   password    ordinationDate  birthday    isValidAge  sex timestamp   ulc_edit_time   osc_sync    guid

【问题讨论】:

  • var_dump $keys$vals,请。
  • $keys$vals 都可以。确定所有字段名称都正确吗?
  • 我从数据库的列列表中添加了一个复制/粘贴供您查看。
  • @GabrielSantos 顶部的联想是通过var_export 清理的垃圾转储
  • 看我的回答,应该能解决这个问题。

标签: php oop pdo associative-array


【解决方案1】:

改变这个:

$i=1;
foreach($clean as $insert) {

    // bindvalue is 1-indexed, so $k+1
    $sth->bindValue($i++, $insert, PDO::PARAM_STR);

    $sth->execute();
    sleep(1);
}

到这里:

$i=1;
foreach($clean as $insert) {

    // bindvalue is 1-indexed, so $k+1
    $sth->bindValue($i++, $insert, PDO::PARAM_STR);

    sleep(1);
}

$sth->execute();

PDO::execute() 需要在所有 bindValues() 的末尾(参见http://php.net/manual/en/pdostatement.execute.php#example-995

另外,我有以下函数来绑定正确的数据类型(需要针对您的情况进行一些更改):

public function bindValue($key = null, $value = null) {
    if($key == null) {
        return;
    }

    if(is_int($value)) {
        $param = PDO::PARAM_INT;
    } elseif(is_bool($value)) {
        $param = PDO::PARAM_BOOL;
    } elseif(is_null($value)) {
        $param = PDO::PARAM_NULL;
    } elseif(is_string($value)) {
        $param = PDO::PARAM_STR;
    } else {
        $param = FALSE;
    }

    $this->_query->bindValue($key, $value, $param);
}

【讨论】:

    猜你喜欢
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 2015-01-06
    • 2015-07-13
    • 2017-01-29
    • 2013-02-17
    相关资源
    最近更新 更多