I have this sqlite3 db:
CREATE TABLE links (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE,
link1 TEXT,
link2 TEXT,
link3 TEXT
);
And I am trying to insert multiple entries in the same query, this is my code:
$db = new PDO('sqlite:db.sqlite');
$sql = "INSERT INTO links VALUES";
$filelines = file('filename');
foreach($filelines as $key => $line)
{
if(count($filelines)-1==$key)
$sql .= "(NULL, '".trim($line)."', '', '', '');";
else
$sql .= "(NULL, '".trim($line)."', '', '', ''),";
}
$insert = $db->prepare($sql);
$insert->execute();
Single inserts works fine but with this code I get the php error:
Call to a member function execute() on a non-object
I’ve also tried creating the sql query like ‘VALUES(…,…), VALUES(…,…),… but get the same error:
What am I doing wrong?
SQLite doesn’t support the
VALUES (...), (...), (...)syntax (see the docs ). So you need to do only one row perINSERTquery…The reason for the error itself is that since there’s a query error,
PDO::prepare()returns false. You should check it to make sure the->prepare()call worked: