• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 419
  • Last Modified:

How to Query Access Database Dates using PHP and MySQL

i have an access database i am connecting to using the below code.  when i use the below query to try to get rows with only a particular date i get no rows returned.  is there some difference in how access stores its date fields vs how mysql queries them?  and if so how do i fix this?

bottom line, how do i extract rows from an access database using php mysql odbc connector filtering using date criteria?

THANKS
$odbc = odbc_connect ('DATABASE', 'username', 'pass') or die('Could    Not Connect to ODBC Database!');
 
$query = odbc_exec($odbc, "select * from Payments where EffDate = 2006-01-03") or die (odbc_errormsg());
 
//this one gives me data type mismatch 
//$query = odbc_exec($odbc, "select * from Payments where EffDate = '2006-01-03'") or die (odbc_errormsg());
 
//this one works fine but is not dates
//$query = odbc_exec($odbc, "SELECT * from Payments WHERE Custno = 652") or die (odbc_errormsg());
 
while($row = odbc_fetch_array($query))
{
	$running_balance = $running_balance + $row['Amount'];
}

Open in new window

0
jasdak
Asked:
jasdak
  • 2
1 Solution
 
TextReportCommented:
There are 2 possible problems.

The first is that you need to use the US date format MM/DD/YYYY rather than YYYY-MM-DD though the latter should be OK.
The second is that the date field can also include the time so your sql needs to be

select * from Payments where EffDate >= '2006-01-03' AND EffDate < '2006-01-04'

Cheers, Andrew
0
 
TextReportCommented:
Oh and it may need to be # around the dates rather than '

select * from Payments where EffDate >= #2006-01-03# AND EffDate < #2006-01-04#

Cheers, Andrew
0
 
jasdakAuthor Commented:
thank you so much.  the pound sign on either side of the date fixed it all.  with quotes it was a type mismatch and without the quotes it would return zero results.  with the pound signs it works perfect.  Thanks!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now