Page 1 of 1

MySQL Query Syntax Question

Posted: Mon Jan 27, 2003 5:19 pm
by mwaw
I have this line in my PHP script:

$delivery = mysql_query("SELECT delivery_city FROM order_summaries WHERE order_number = '$_SESSION[CartNumber]'");

It works fine, but I believe that proper syntax would require a quote marks around the Session array key CartNumber. I am worried that someday, somewhere this line won't work because it does not have proper syntax.

I've tried escaping the various quote marks in this line, but that breaks the code.

Any suggestions?

Thanks!

Posted: Mon Jan 27, 2003 5:36 pm
by mydimension
there are a few ways to do it. here is my favorite:

Code: Select all

$delivery = mysql_query("SELECT delivery_city FROM order_summaries WHERE order_number = '{$_SESSIONї'CartNumber']}'");
anything within the {} is considered a variable so you can quote the key name as usual.

Posted: Mon Jan 27, 2003 10:45 pm
by phice
I learn something everyday.

No more $id = $_POST["id"]; lines anymore ;)

Posted: Tue Jan 28, 2003 2:17 am
by twigletmac
Within double quoted strings there is no need to quote your array elements so:

Code: Select all

$sql = "SELECT delivery_city FROM order_summaries WHERE order_number = '$_SESSION[CartNumber]'";
is just as valid syntactically as:

Code: Select all

$sql = "SELECT delivery_city FROM order_summaries WHERE order_number = '{$_SESSION['CartNumber']}'";
or

Code: Select all

$sql = "SELECT delivery_city FROM order_summaries WHERE order_number = '".$_SESSION['CartNumber']."'";
For more info:
http://www.php.net/manual/en/language.types.array.php
http://www.php.net/manual/en/language.t ... ng.parsing

Mac

THANKS!

Posted: Tue Jan 28, 2003 10:34 am
by mwaw
Thanks - I appreciate the feedback!