Query Not Returning Correct Records

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Harlequin
Forum Commoner
Posts: 51
Joined: Tue Sep 21, 2004 10:51 am
Location: UK

Query Not Returning Correct Records

Post by Harlequin »

Hi everyone.

This is the issue:

I have a variable: "$LastDownload" which is the last record in a table that identifies the last time a particular query was executed.

I also have a column in another table called "LastAction" which indicates the last time a user updated their information.

Both fields are Date/Time fields and capture the date in the following format:

Code: Select all

$Time = date("Y-m-d H:i:s");
For some reason, the following query returns ALL records though, even those whose LastAction Date is less than the $LastDownload date:

Code: Select all

// Select ALL Changed Records: 
   $UserDataQry = "SELECT * FROM MembersData 
   WHERE 'LastAction' > '$LastDownload' 
   ORDER BY UserID"; 
    $UserData = mysql_query($UserDataQry) or die("Error: " . mysql_error());
I'd appreciate any help you guys can offer here as I just can't seem to get the query right.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it looks like LastAction is a string in your query, not a column reference.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you should use backticks for columns ... eg. `lastaction`
Harlequin
Forum Commoner
Posts: 51
Joined: Tue Sep 21, 2004 10:51 am
Location: UK

Post by Harlequin »

Thanks very much Phenom

I knew I was missing something simple.

Would it be better syntactically if I used:

Code: Select all

WHERE ' . `LastAction` . ' > ' . $LastDownload . '
Instead of:

Code: Select all

WHERE `LastAction` > '$LastDownload'
I appreciate your help on this, been trying to get these CSV downloads automated properly for over a week and now t's done - thanks again mate 8)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

WHERE `LastAction` > '$LastDownload'
is fine..

you don't have to escape single quotes for variables in your queries
Harlequin
Forum Commoner
Posts: 51
Joined: Tue Sep 21, 2004 10:51 am
Location: UK

Post by Harlequin »

:D Thanks again :D
Post Reply