0

我正在尝试使用单个准备好的语句插入多行,但我无法使其工作。

我创建了以下辅助方法:

// generate "(?, ?, ?, ?), (?, ?, ?, ?)" from cols and values
// to be used in a prepared statement
public function placeholders ($values, $cols) {
    $row = '('.implode(',', array_fill(0, count($cols), '?')).')';
    $place_holders = array_fill(0, count($values)/count($cols), $row);
    return implode(',', $place_holders);
}

// generate the "?" placeholders and make the PDO transaction
protected function insertInto ($table, array $cols, array $values) {
    $query = 'INSERT INTO `'.$table.'` (`'.implode('`,`', $cols).'`) VALUES '.$this->placeholders($values, $cols);
    return $this->makeTransaction($query, $values);
}

// prepare and execute a PDO transaction
protected function makeTransaction ($query, array $values=array()) {
    $this->pdo->beginTransaction();
    $q = $this->pdo->prepare($query);
    $q->execute($values);
    return $this->pdo->commit();
}

至于调用本身,这里是:

$this->insertInto('table', array('f_key', 'c2', 'c3'), $values);

我正在使用以下 $values 数组:

$values = array(1, 'a', 'b', 2, 'c', 'd', ...);

现在数据没有进入表格,但同时我没有得到任何异常或任何类型的错误。我在通话后尝试过var_dump$this->pdo->errorInfo()但它也没有错误。任何想法?

4

0 回答 0