Jump to content

A simple query with single and double quote problem


IreneLing

Recommended Posts

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.

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.

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

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.