0

I'm trying to extract data from a table in access database using odbc connection and php. I have written the code below but it gives error "Uncaught exception 'com_exception' with message 'Source: Microsoft OLE DB Provider for ODBC Drivers
Description: [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'table1.col_date"

<?php $cn = new com("ADODB.Connection");
 $rs = new com("ADODB.Recordset");

$cn->open("dsn=odbcconnection");
?>
<form action = "thispage.php" method = "post">
Enter Date : <input type = "text" name = "datadate" />
<input type = "submit" name = "submit" value = "submit" /> 
</form>
<?php
$datadate = isset($_REQUEST['datadate']) ? $_REQUEST['datadate'] : null; ?>

<?php $sql = "select col_date, sum(qty1), sum(qty2) from table1
              where  table1.col_date = '".$datadate."'
              group by col_date
              order by col_date";

// Execute query
$rs = $cn->execute($sql);

I think there is a problem in only parameter line table1.col_date = $datadate because when I replace $datadate with static date like table1.col_date = #05/08/2012# , it displays output correctly for the date

6
  • and what about format? is table.col_date type date? Commented Aug 29, 2012 at 8:44
  • you should try yyy-mm-dd Commented Aug 29, 2012 at 8:46
  • im getting this error even before selecting date..so I don't think it is related to format...i tried changing table1.col_date = '$datedate' but still the same but if I change it to table1.col_date = #01/05/2012#, it correctly displays output for 1st May, 2012 Commented Aug 29, 2012 at 8:50
  • $_REQUEST['datadate'] suggests a query string parameter. Is that populated and spelt correctly? Are you posting to this page or an actual thispage.php file? Commented Aug 29, 2012 at 9:06
  • yes it is spelled correctly..Now I'm getting error "data type mismatch" after editing code as per your answer with double quotes(i updated in question) Commented Aug 29, 2012 at 9:09

1 Answer 1

1

The problem is that is either you are using query string parameters posting to the current page which are empty or you are getting confused with POST. Try the following

If you are posting to the page itself then do the following

<form action = "<?php echo $PHP_SELF; ?>" method = "post"> 

then change the the to the following:

$datadate = isset($_POST['datadate']) ? $_POST['datadate'] : null; ?>

Lastly make the change as previously stated

Replace the [table1.col_date] with [table1].[col_date] or remove the brackets completely and try the following

<?php $sql = "select col_date, qty1, qty2 from table1 where  [table].[col_date]= '".$datadate."' group by col_date order by col_date"; 
Sign up to request clarification or add additional context in comments.

3 Comments

still I'm getting Syntax error (missing operator) in query expression 'table1.col_date = '.'..please see original question i edited my code above as per your answer.....
Thanks JLC007 for your help but it doesn't seem to work. I will keep trying anyway.
After a long effort, i managed to make it work. I used [table].[col_date] = #$datadate#

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.