duansha3771 2018-05-10 08:14
浏览 55
已采纳

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

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.

I see also the bindParam() or bindValue return false if an error exist. 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.

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条回答 默认 最新

  • duanlanzhi5509 2018-05-10 08:24
    关注

    The variable $stmt is not a PDOStatement object. It is a boolean value (in this case false).

    Your INSERT statement is not valid. Try the following instead (missing 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.

    $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:

    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. Use the following CREATE TABLE statement:

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析