绑定参数在循环内不起作用


bindParam not working right inside loop

你好,我有以下函数和以下类。问题是它只绑定数组中的一个键,而不是所有键,而且我不知道为什么。

public function addData(Array $data)
{
    $this->beginTransaction();
    $this->query('INSERT INTO test (name, date) VALUES (:invoiceName, NOW())');
    $this->bind(':invoiceName', uniqid());
    $this->execute();
    $this->query('INSERT INTO test_table (testId, username, type) VALUES (:invoiceId, :username, :type, )');
    $this->bind(':invoiceId', $this->lastInsertId());
    foreach($data as $key => $value)
    {
        $this->bind(':username', $value['name']);
        $this->bind(':type', $value['type']);
    }
    $this->execute();
    $this->endTransaction();
}

以及我将其用于数据库连接的类。

<?php
use PDO;
use PDOException;
class Database
{
    private
        $pdoOptions = array
            (
                PDO::ATTR_PERSISTENT => true,
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                PDO::ATTR_PERSISTENT => true,
            );
    private 
        $connectionConfig = array
            (
                'dsn' => 'mysql:dbname=Test;host=127.0.0.1',
                'username' => 'root',
                'password' => ''
            );
    private $dbh;
    private $stmt;
    public function setOptions($pdoOptions = array())
    {
        $this->pdoOptions = array_merge($this->pdoOptions, $pdoOptions);
    }
    public function getOptions()
    {
        return $this->pdoOptions;
    }
    public function setConnectionConfig($connectionArray = array())
    {
        $this->connectionConfig = array_merge($this->connectionConfig, $connectionArray);
    }
    public function getConnectionConfig()
    {
        return $this->connectionConfig;
    }
    public function createInstance()
    {
        if (!extension_loaded('PDO'))
            throw new PDOException(__CLASS__ . ': The PDO extension is required for this class');
        try {
            $this->dbh = new PDO($this->connectionConfig['dsn'], $this->connectionConfig['username'], $this->connectionConfig['password'], $this->pdoOptions);
        } catch (PDOException $e) {
            $this->error = $e->getMessage();
        }
    }
    public function query($query)
    {
        $this->stmt = $this->dbh->prepare($query);
    }
    public function bind($param, $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_INT;
                    break;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    break;
                default:
                    $type = PDO::PARAM_STR;
            }
        }
        $this->stmt->bindValue($param, $value, $type);
    }
    public function execute()
    {
        $this->stmt->execute();
    }
    public function resultSet($type = PDO::FETCH_ASSOC)
    {
        $this->execute();
        return $this->stmt->fetchAll($type);
    }
    public function singleResult($type = PDO::FETCH_ASSOC)
    {
        $this->execute();
        return $this->stmt->fetch($type);
    }
    public function rowCount()
    {
        return $this->stmt->rowCount();
    }
    public function lastInsertId()
    {
        return $this->dbh->lastInsertId();
    }
    public function beginTransaction()
    {
        $this->dbh->beginTransaction();
    }
    public function endTransaction()
    {
        $this->dbh->commit();
    }
    public function cancelTransaction()
    {
        $this->dbh->rollBack();
    }
    public function debugDumpParams()
    {
        return $this->stmt->debugDumpParams();
    }
}

你需要像这样将 execute 放在 foreach 循环中:

foreach($data as $key => $value)
{
    $this->bind(':username', $value['name']);
    $this->bind(':type', $value['type']);
    $this->execute();
}