0

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.

vista1x
  • 1
  • 1
  • I'm not following this 100% (plus I've literally never used MS Access), but are you saying that testQuery is a stored procedure, and you're trying to call it with a parameter? @chris85's answer below is fine if testQuery is a table, but from your post, I'm thinking perhaps it's not a table (Tours is the table). – Rob G Aug 21 '15 at 17:58
  • Yes, testQuery - saved query (not procedure) and I want to call it with a parameter – vista1x Aug 21 '15 at 18:10
  • sorry can't really help with MS Access stuff, but from your error message, this might be useful: http://php.net/manual/en/function.odbc-execute.php (there is a user comment on there of 'A quick note in hopes that my pain will save someone else: Microsoft Access ODBC drivers do not support parameterized queries.' but this was 12 years ago so has probably changed!) – Rob G Aug 21 '15 at 18:14

1 Answers1

1

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.

chris85
  • 23,846
  • 7
  • 34
  • 51
  • This does not work. testQuery - is prepared (stored) a request to the database MS Access. When you run it directly into the database window will appear with a request parameter "tour." – vista1x Aug 21 '15 at 18:09
  • 2
    In your current example there is no reference to `:tour` so it has no reason to being binding. I'm not familiar with how ms-access executes a stored procedure. Maybe this thread would be useful for executing a stored procedure paramterized, http://stackoverflow.com/questions/31575135/how-to-execute-a-stored-procedure-in-php-using-sqlsrv-and-style-parameters/31591374#31591374 – chris85 Aug 21 '15 at 18:31
  • @vista1x any luck with that? – chris85 Aug 21 '15 at 19:03
  • No, it is not working. It seems that such actions are not supported by all ODBC-driver. Thanks for the help! – vista1x Aug 21 '15 at 19:14