2

I am working with sqlite for the first time.

Preparing a query string like

$articleInsertQuery = "INSERT INTO Articles VALUES (?, ?, ?, ?, ?)", ($i, $title, $content, $date, 93,);

It returns "Parse error". I also tried without passing parametrized query like

$articleInsertQuery = "INSERT INTO Articles VALUES ($i, $title, $content, $date, 93)";

ANd getting "Unable to prepare statement: 1, unrecognized token: ":" "

Any idea where I am doing wrong?

j0k
  • 22,600
  • 28
  • 79
  • 90
arnold
  • 1,682
  • 8
  • 24
  • 31

1 Answers1

2

@arnoldIf you are using PDO for that.

The way to prepare and execute your query would be as follows.

$dbObject = new PDO('sqlite:sqlitedb');// NEW LINE
$articleInsertQuery = "INSERT INTO Articles VALUES (?, ?, ?, ?, ?)";
$query = $dbObject->prepare($articleInsertQuery);
$query->execute(array($i, $title, $content, $date, 93));

EDIT:

See sqlite3 prepare.

$articleInsertQuery = "INSERT INTO Articles VALUES (:i, :title, :content, :date, :int)";
$query = $dbObject->prepare($articleInsertQuery);
$query->bindValue(':i', $i, SQLITE3_INTEGER);
$query->bindValue(':title', $title, SQLITE3_TEXT);
$query->bindValue(':content', $content, SQLITE3_TEXT);
$query->bindValue(':date', $date, SQLITE3_TEXT);
$query->bindValue(':int', 93, SQLITE3_INTEGER);    

$result = $query->execute();

Hope this helps.

Luigi Siri
  • 2,068
  • 2
  • 19
  • 27