IreneLing Posted December 24, 2011 Share Posted December 24, 2011 I'm so sorry for this question but I not really know how to play with single and double quote. If I have a query like this: mysql_query('UPDATE table SET Status=1,Sending=Done WHERE ID IN ('.implode(',', $done).')'); And I wish to add SentAt='$date' in the query as well , and I try this: mysql_query('UPDATE table SET Status=1,Sending=Done,SentAt='$date' WHERE ID IN ('.implode(',', $done).')'); Not working...how should I write it? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/253800-a-simple-query-with-single-and-double-quote-problem/ Share on other sites More sharing options...
AGuyWithAthing Posted December 24, 2011 Share Posted December 24, 2011 In php a string in single quotes is a constant value ( is exactly what you write ) and a string in double quotes is a dynamic string ( checks for variables and escaping values ). so mysql_query('UPDATE table SET Status=1,Sending=Done,SentAt='$date' WHERE ID IN ('.implode(',', $done).')'); Won't work as the value of '$date' is breaking the syntax as you have closed the string had no operation then added a variable then opened a new constant string. To fix this you can simply do : mysql_query('UPDATE table SET Status=1,Sending=Done,SentAt=' . $date . ' WHERE ID IN ('.implode(',', $done).')'); Notice the . either side of the $date variable which concatenates string values. Another way would have been to use double quotes e.g. : mysql_query( "UPDATE table SET Status=1,Sending=Done,SentAt='$date ' WHERE ID IN ( " . implode(',', $done) . ')' ); Both should work. Quote Link to comment https://forums.phpfreaks.com/topic/253800-a-simple-query-with-single-and-double-quote-problem/#findComment-1301153 Share on other sites More sharing options...
litebearer Posted December 24, 2011 Share Posted December 24, 2011 an aside... It may be better to create your queries as strings. Doing that allow you to echo the queries when checking to ascertain that produce values you are expecting. AND, for me atleast, makes it easier to use single and double quotes. ie... $query = "something"; $result = mysql_query($query); Quote Link to comment https://forums.phpfreaks.com/topic/253800-a-simple-query-with-single-and-double-quote-problem/#findComment-1301161 Share on other sites More sharing options...
IreneLing Posted December 25, 2011 Author Share Posted December 25, 2011 In php a string in single quotes is a constant value ( is exactly what you write ) and a string in double quotes is a dynamic string ( checks for variables and escaping values ). so mysql_query('UPDATE table SET Status=1,Sending=Done,SentAt='$date' WHERE ID IN ('.implode(',', $done).')'); Won't work as the value of '$date' is breaking the syntax as you have closed the string had no operation then added a variable then opened a new constant string. To fix this you can simply do : mysql_query('UPDATE table SET Status=1,Sending=Done,SentAt=' . $date . ' WHERE ID IN ('.implode(',', $done).')'); Notice the . either side of the $date variable which concatenates string values. Another way would have been to use double quotes e.g. : mysql_query( "UPDATE table SET Status=1,Sending=Done,SentAt='$date ' WHERE ID IN ( " . implode(',', $done) . ')' ); Both should work. Thank you so much, yes it works.Really thanks to your solution and explanations. Quote Link to comment https://forums.phpfreaks.com/topic/253800-a-simple-query-with-single-and-double-quote-problem/#findComment-1301273 Share on other sites More sharing options...
IreneLing Posted December 25, 2011 Author Share Posted December 25, 2011 an aside... It may be better to create your queries as strings. Doing that allow you to echo the queries when checking to ascertain that produce values you are expecting. AND, for me atleast, makes it easier to use single and double quotes. ie... $query = "something"; $result = mysql_query($query); Thanks for your advice , I will take note of it , thank you. Quote Link to comment https://forums.phpfreaks.com/topic/253800-a-simple-query-with-single-and-double-quote-problem/#findComment-1301274 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.