newbiePHP Posted October 23, 2007 Share Posted October 23, 2007 hey guys, I am trying to sort out an SQL statement. The below statement will return nothing when it's executed. $date="2007-08-28 15:24:44"; $sql = 'select * from example order by abs( unix_timestamp(`$date`)- unix_timestamp(datetime) ) limit 1'; The problem is when I hardcode in vehicledate,(see below) it works just fine. $sql = 'select * from example order by abs( unix_timestamp("2007-08-28 15:24:44")- unix_timestamp(datetime) ) limit 1'; I assume there is some problem passing variables into SQl statements. Does anyone know how to get this to work. In my PHP script I will be taking in the date from a website so hardcoding it in is not an option. Any help would be appreciated. Thanks Link to comment https://forums.phpfreaks.com/topic/74434-passing-variable-to-sql-query/ Share on other sites More sharing options...
enoyhs Posted October 23, 2007 Share Posted October 23, 2007 It is because variables won't work in single quotes. $sql = 'select * from example order by ' . abs( unix_timestamp('$date')- unix_timestamp(datetime) ) . ' limit 1'; Link to comment https://forums.phpfreaks.com/topic/74434-passing-variable-to-sql-query/#findComment-376053 Share on other sites More sharing options...
newbiePHP Posted October 23, 2007 Author Share Posted October 23, 2007 I tried that but it gave me this error Fatal error: Call to undefined function unix_timestamp() in C:\wamp\www\test1.php Link to comment https://forums.phpfreaks.com/topic/74434-passing-variable-to-sql-query/#findComment-376062 Share on other sites More sharing options...
enoyhs Posted October 23, 2007 Share Posted October 23, 2007 That must be because unix_timestamp() is SQL function... Try this: $sql = "select * from example order by abs( unix_timestamp('$date')- unix_timestamp(datetime) ) limit 1"; Link to comment https://forums.phpfreaks.com/topic/74434-passing-variable-to-sql-query/#findComment-376064 Share on other sites More sharing options...
newbiePHP Posted October 23, 2007 Author Share Posted October 23, 2007 That worked beautifully. great job Thanks a lot Link to comment https://forums.phpfreaks.com/topic/74434-passing-variable-to-sql-query/#findComment-376067 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.