[PHP-DB] Syntax Problem (Was : Date problem)

2007-01-21 Thread Neil Smith [MVP, Digital media]

At 16:26 21/01/2007, you wrote:


I have a date field in mysql called event_end .

I want to run a query to find all records where the event_and is greater
than today's date. I have written the following code. It does not work.
Please point out the mistake.

$today = getdate();
 $sql=select * from events where event_end'.$today.' order by event_start
Asc ;



Because PHP allows you to quote variables inside strings surrounded 
by   characters, your string $sql passed to the database will 
actually be as follows


select * from events where event_end'.array.' order by event_start

which you would have seen if you had used print($sql) to debug this. 
The reason if prints array is because - well getdate() returns an 
array, which you would have seen if you'd RTFM : array getdate ( [int 
timestamp] ).


And there are dots surrounding the word `array` because - well, you 
put them there and used single quotes to ensure they were output 
literally in the string, rather than closing the string using a 
matching double quote character then re-opening it with another 
double quote character after the $today variable.



Probably I would use the date() function  to generate that date as a 
*string*, and make sure I quoted that string correctly when making 
the $sql string, eg like


$today = date('Y/m/d');
$sql = SELECT * FROM events WHERE event_end '.$today.' ORDER BY 
event_start ASC;


Cheers - Neil  


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] Date problem

2007-01-21 Thread Miles Thompson

At 12:26 PM 1/21/2007, Denis L. Menezes wrote:


Dear friends.

I have a date field in mysql called event_end .

I want to run a query to find all records where the event_and is greater
than today's date. I have written the following code. It does not work.
Please point out the mistake.

$today = getdate();
 $sql=select * from events where event_end'.$today.' order by event_start
Asc ;


Thanks
denis


How is your date formatted in the database.

Compare  that to the format returned by getdate(), then consider using 
date('Y-m-d'). That's assuming your MySQL date is stored as -mm-dd.


Nonetheless, this should set you on the right road.

It would also have helped your diagnosis if you echoed your SQL statement.

Cheers - Miles Thompson




--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.410 / Virus Database: 268.17.3/642 - Release Date: 1/20/2007

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php