3

I have a Microsoft Access database with a bunch of saved queries. I would like to use PHP to run and grab the results of these queries. I can query tables in the same database just fine. My understanding is that I can treat queries as if they were tables.

This will return data from the table "New Grower Fields" just fine:

$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=$connString; SystemDB=$mdw; Uid=$username; Pwd=$password");
$sth = $db->prepare("SELECT * FROM [New Grower Fields]");
$sth->execute();
$results = $sth->fetchALL(PDO::FETCH_ASSOC);
print_r($results);

But if I want to use a saved query, which I believe should act just like querying a table, I don't get anything.

$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=$connString; SystemDB=$mdw; Uid=$username; Pwd=$password");
$sth = $db->prepare("SELECT * FROM [Daily Tonnage by Plant]");
$sth->execute();
$results = $sth->fetchALL(PDO::FETCH_ASSOC);
print_r($results);

Is there any way to allow me to get the results of a saved query in MS Access with PHP? I'm fairly new to this. I appreciate any and all help! I will be happy to provide any additional information needed.

10
  • 2
    OK first up, a bit of general advice, don't put spaces in object names in an access db. Secondly there is no reason why that wouldn't work as long as you have permission to access the query (and you spelled it right, of course) - just as a quick sanity check I've just tested it on my own system and I can indeed retrieve records from queries in exactly the same way as I can from tables. Add $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); as the second line and see if it spits out a useful error message.
    – DaveRandom
    Oct 2, 2012 at 17:09
  • @DaveRandom I unfortunately have no control over the access db; I can only read. I tried your suggestion and it gave me the error msg: "PDOException: SQLSTATE[07002]: COUNT field incorrect: -3010 [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1. (SQLExecute[-3010] at ext\pdo_odbc\odbc_stmt.c:254) in C:\wamp\www\getDataGL.php on line 5." Line 5 being $sth->execute();. I'll continue searching, but I'm not entirely sure what that means.
    – delucis
    Oct 2, 2012 at 17:43
  • @DaveRandom You know what? I just tried it with a different saved query and it worked fine. Here is the SQL from the query that errors: SELECT [collect plant and date for reports].plant, [collect plant and date for reports].[Grade Date], Sum([NetWt]/2000) AS NetTon, Sum([NetWt]*[Perc1]/2000) AS Ton1, Sum([NetWt]*[Perc2S]/2000) AS Ton2S, Sum([NetWt]*[Perc3]/2000) AS Ton3, Sum([NetWt]*[PercCull]/2000) AS TonCull, Sum([NetWt]*[Perc2O]/2000) AS Ton2O FROM [collect plant and date for reports] GROUP BY [collect plant and date for reports].plant, [collect plant and date for reports].[Grade Date];
    – delucis
    Oct 2, 2012 at 17:48
  • OK it sounds like the query is expecting user input when it's run in Access and this is much more difficult to diagnose without actually having the file. What happens if you run the query in Access? Does it prompt you for a value before showing you the results? If so, what is the message displayed in the prompt - it should tell you the field name. Can you show the column list from the (horribly named) collect plant and date for reports table?
    – DaveRandom
    Oct 2, 2012 at 19:47
  • I think I know what the issue is. We use a remote desktop connection to open our database upon connection like this: "C:\Program Files\Microsoft Office\Office\msaccess.exe" "\\server\path\Grower Loads\Live2012\Converted_GrowerScheduling.mdb" /wrkgrp \\server\path\etc\NETWORKGRP.mdw /cmd Toledo. My connection string is "odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=\\\\server\\path\\Grower Loads\\Live2012\\Converted_GrowerScheduling.mdb; SystemDB=\\\\server\\path\\etc\\NETWORKGRP.mdw;Uid=$name;Pwd=$pwd"The /cmd Toledo is the important part, need to put in connection string somehow?
    – delucis
    Oct 2, 2012 at 22:02

1 Answer 1

0

This seemed to solve it for me:

$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=$connString; SystemDB=$mdw; Uid=$username; Pwd=$password");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $db->prepare("SELECT [Grade Date], NetWt FROM [Daily Tonnage by Plant]");
$sth->execute();

while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
  print_r($row);
}

I added $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); and changed

$results = $sth->fetchALL(PDO::FETCH_ASSOC); to $row = $sth->fetch(PDO::FETCH_ASSOC)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.