gpt4 book ai didi

php - Sqlite 调用 bool 值上的成员函数 bindParam()

转载 作者:行者123 更新时间:2023-12-03 17:46:42 25 4
gpt4 key购买 nike

你好,我尝试使用 PDO 将数据插入到 Sqlite,我尝试了很多方法,但我总是遇到以下错误:在 bool 值上调用成员函数 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 这就是 Create 语句。

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);
}

}

最佳答案

变量$stmt 不是PDOStatement目的。它是一个 bool 值(在本例中为 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();
}

您还可以在初始化之前使用 $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 的组合是唯一的,您需要向表 costumers 添加 UNIQUE 约束。使用以下 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

关于php - Sqlite 调用 bool 值上的成员函数 bindParam(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50268448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com