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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.