Question

I'm trying to use prepared statements in a PHP script that accesses an SQLite3 database using PDO.

The normal version of this query works as expected:

$q1 = "SELECT COUNT(*) FROM fruits WHERE name='apple'";
echo $db->query($q1)->fetchColumn();

However, my prepared statement version outputs nothing.

$q2 = "SELECT COUNT(*) FROM fruits WHERE name='?'";
$s = $db->prepare($q2);
$s->execute("apple");
echo $s->fetchColumn();

What am I doing wrong? I tried with both PHP 5.2 and PHP 5.3, with the same results.

Was it helpful?

Solution

I think you don't need additional apostrophes before and after the ? and you should use an array: $s->execute(array("apple"));

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top