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 Arduino,利用modbus的RS485协议,进行对外置的温湿度传感器进行数据读取
  • ¥15 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么