简体   繁体   English

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

[英]Sqlite Call to a member function bindParam() on boolean

Hello I try with PDO to insert data to Sqlite, i have tried many ways, but I always get following errors: Call to a member function bindParam() on boolean. 您好,我尝试使用PDO将数据插入Sqlite,我尝试了很多方法,但是我总是遇到以下错误:在布尔值上调用成员函数bindParam()。

I see also the bindParam() or bindValue return false if an error exist. 我还看到,如果存在错误,则bindParam()或bindValue返回false。 But I don't find an error. 但我没有发现错误。

thx in advance 提前

    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 Thats the Create Statement. @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);
     }

}

The variable $stmt is not a PDOStatement object. 变量$stmt不是PDOStatement对象。 It is a boolean value (in this case false ). 它是一个布尔值(在本例中为false )。

Your INSERT statement is not valid. 您的INSERT语句无效。 Try the following instead (missing OR ): 请尝试以下操作(缺少OR ):

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

You can use the methods PDO::errorInfo and PDO::errorCode to get further information. 您可以使用方法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();
}

You also use $firstName and $lastName before 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();
    }
}

To make sure the combination of first_name and last_name is unique, you need to add a UNIQUE constraint to your table costumers . 为了确保first_namelast_name的组合是唯一的,您需要向表costumers添加UNIQUE约束。 Use the following CREATE TABLE statement: 使用以下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)
);

You can see the difference with and without the UNIQUE constraint on these following demo: http://sqlfiddle.com/#!7/79b1c/1/1 您可以在以下这些演示中看到有无UNIQUE约束的区别: http : //sqlfiddle.com/#!7/79b1c/1/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM