Sqlite在boolean上调用成员函数bindParam()

时间:2018-05-10 08:14:53

标签: php sqlite pdo

您好我尝试使用PDO向Sqlite插入数据,我尝试了很多方法,但我总是遇到以下错误:在布尔值上调用成员函数bindParam()。

如果存在错误,我也看到bindParam()或bindValue返回false。但我没有发现错误。

事先提前

    function insertCostumers(){
    $costumers = 'INSERT IGNORE INTO costumers(first_name,last_name,age)
            VALUES(:first_name,:last_name,:age)';
    $stmt = $this->pdo->prepare($costumers);
    $data = [['firstName' => 'Hans',
            'lastName' => 'Meier',
            'age' => 32],
            ['firstName' => 'Anna',
            'lastName' => 'Mueller',
            'age' => 35],
            ['firstName' => 'Steffi',
            'lastName' => 'Gygax',
            'age' => 67]];

        $stmt->bindParam(
            ':first_name', $firstName,
            ':last_name', $lastName,
            'age', $age);
        foreach ($data as $d) {
             // Set values to bound variables
            $firstName = $d['firstName'];
            $lastName = $d['lastName'];
            $age = $d['age'];
            // Execute statement
            $stmt->execute();
        }
        die('hello');
}


require "SQLiteConnection.php";
require "SQLiteCreateTable.php";

$sqlite = new SQLiteCreateTable((new SQLiteConnection())->connect());
// create new tables
$sqlite->createTables();
$sqlite->insertCostumers();
$tables = $sqlite->getOrderList();
require "index.view.php";

@SebastianBrosch就是创建声明。

public function createTables() {
$commands = ['CREATE TABLE IF NOT EXISTS costumers (
                costumer_id integer PRIMARY KEY,
                first_name text NOT NULL,
                last_name text NOT NULL,
                age integer NOT NULL
              )',
    'CREATE TABLE IF NOT EXISTS orders (
            order_id integer PRIMARY KEY,
            order_nr integer NOT NULL,
            costumer_id integer,
            FOREIGN KEY (costumer_id) REFERENCES costumers (costumer_id) 
            ON DELETE CASCADE ON UPDATE NO ACTION)'];
     // execute the sql commands to create new tables
     foreach ($commands as $command) {
        $this->pdo->exec($command);
     }

}

1 个答案:

答案 0 :(得分:3)

变量$stmt不是PDOStatement对象。它是一个布尔值(在本例中为false)。

您的INSERT声明无效。请尝试以下方法(缺少OR):

$costumers = 'INSERT OR IGNORE INTO costumers(first_name, last_name, age)
        VALUES(:first_name, :last_name, :age)';

您可以使用方法PDO::errorInfoPDO::errorCode获取更多信息。

$costumers = 'INSERT OR IGNORE INTO costumers(first_name,last_name,age)
        VALUES(:first_name,:last_name,:age)';
$stmt = $this->pdo->prepare($costumers);

if ($stmt === false) {
     echo $this->pdo->errorCode().': '.$this->pdo->errorInfo();
}

您还可以在init:

之前使用$firstName$lastName
function insertCostumers() {
    $costumers = 'INSERT OR IGNORE INTO costumers(first_name, last_name, age)
        VALUES(:first_name, :last_name, :age)';
    $stmt = $this->pdo->prepare($costumers);
    $data = [['firstName' => 'Hans',
        'lastName' => 'Meier',
        'age' => 32],
        ['firstName' => 'Anna',
        'lastName' => 'Mueller',
        'age' => 35],
        ['firstName' => 'Steffi',
        'lastName' => 'Gygax',
        'age' => 67]];

     foreach ($data as $d) {
         $firstName = $d['firstName'];
         $lastName = $d['lastName'];
         $age = $d['age'];

         $stmt->bindParam(':first_name', $firstName, PDO::PARAM_STR);
         $stmt->bindParam(':last_name', $lastName, PDO::PARAM_STR);
         $stmt->bindParam(':age', $age, PDO::PARAM_INT);

        $stmt->execute();
    }
}

要确保first_namelast_name的组合是唯一的,您需要在表UNIQUE中添加costumers约束。使用以下CREATE TABLE声明:

CREATE TABLE IF NOT EXISTS costumers (
    costumer_id INTEGER PRIMARY KEY,
    first_name TEXT NOT NULL,
    last_name TEXT NOT NULL,
    age INTEGER NOT NULL,
    UNIQUE (first_name, last_name)
);
  

您可以在以下演示中看到有UNIQUE约束的区别和不区分:   http://sqlfiddle.com/#!7/79b1c/1/1