vista1x
vista1x

Reputation: 1

How bind parameters for a prepared query in PDO?

There is a database in MS Access, has it prepared (stored) with the name of testQuery:

PARAMETERS tour Number;
SELECT * FROM Tours WHERE ID = tour;

I am trying to run a query in PHP and pass to tour the parameter as follows:

// ..connect
$tour = 1;
$sth = $db->prepare("select * from testQuery");
$sth->bindParam(':tour', $tour, PDO::PARAM_INT);
$sth->execute();

I get the error:

SQLSTATE[07002]: COUNT field incorrect: -3010 [Microsoft][Driver ODBC Microsoft Access]
Too few parameters. Required 1. (SQLExecute [-3010] at ext \ pdo_odbc \ odbc_stmt.c: 254)

How to pass parameters to the prepared statement? There is a suspicion that prepared queries are not caused by the command "select", as well as something else.

Upvotes: 0

Views: 181

Answers (1)

chris85
chris85

Reputation: 23892

In your current example no parameters would be expected. You need to pass the where clause in the query. You just use placeholders where the user input would go. So for example

$tour = 1;
$sth = $db->prepare("select * from testQuery WHERE ID = :tour");
$sth->bindParam(':tour', $tour, PDO::PARAM_INT);
$sth->execute();

Another approach is

$tour = 1;
$sth = $db->prepare("select * from testQuery WHERE ID = ?");
$sth->execute(array($tour));

You can see more examples on the manual's page, http://php.net/manual/en/pdo.prepare.php.

Upvotes: 1

Related Questions