Hello,
Is this the right syntax for assigning a sql selct to a variable. When
I use this I get a parse error on the line that this is on.
$thecustid = 'SELECT custid FROM customers WHERE userid =
$_SESSION['MM_Username']';
Thanks,
td.
QUOTE |
Hello, Is this the right syntax for assigning a sql selct to a variable. When I use this I get a parse error on the line that this is on. $thecustid = 'SELECT custid FROM customers WHERE userid = $_SESSION['MM_Username']'; |
QUOTE |
$thecustid = 'SELECT custid FROM customers WHERE userid = $_SESSION['MM_Username']'; Using single qoutes in this case is wrong because a) you've got single qoutes inside single qoutes, and that my freind, makes php go apeshit. b) it doesn't interpret variables inside a string if the string is delimited by single qoutes. Throw in some double qoutes: $thecustid = "SELECT custid FROM customers WHERE userid = $_SESSION['MM_Username']"; And that should work. |
QUOTE |
On 16-Jun-2005, "Kimmo Laine" <[Email Removed]> wrote: $thecustid = 'SELECT custid FROM customers WHERE userid = $_SESSION['MM_Username']'; Using single qoutes in this case is wrong because a) you've got single qoutes inside single qoutes, and that my freind, makes php go apeshit. b) it doesn't interpret variables inside a string if the string is delimited by single qoutes. Throw in some double qoutes: $thecustid = "SELECT custid FROM customers WHERE userid = $_SESSION['MM_Username']"; And that should work. Actually you need to remove the single quotes from around MM_Username (or add {} braces around the whole variable), too. If userid is not numeric, you need to add single quotes around its value to make the SQL correct, so you get: $thecustid = "SELECT custid FROM customers WHERE userid = '$_SESSION[MM_Username]' "; |
QUOTE |
http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing |
QUOTE |
Tom Thackrey wrote: On 16-Jun-2005, "Kimmo Laine" <[Email Removed]> wrote: $thecustid = 'SELECT custid FROM customers WHERE userid = $_SESSION['MM_Username']'; Using single qoutes in this case is wrong because a) you've got single qoutes inside single qoutes, and that my freind, makes php go apeshit. b) it doesn't interpret variables inside a string if the string is delimited by single qoutes. Throw in some double qoutes: $thecustid = "SELECT custid FROM customers WHERE userid = $_SESSION['MM_Username']"; And that should work. Actually you need to remove the single quotes from around MM_Username (or add {} braces around the whole variable), too. If userid is not numeric, you need to add single quotes around its value to make the SQL correct, so you get: $thecustid = "SELECT custid FROM customers WHERE userid = '$_SESSION[MM_Username]' "; You shouldn't mix string keys and constanst. So instead of " $_SESSION[MM_Username] " (looks for a constant named "MM_Username" and if it doesn't find it, uses 'MM_Username' as a string key) use " {$_SESSION['MM_Username']} " as suggested in the manual. http://www.php.net/manual/en/language.type....string.parsing -veikko |
QUOTE |
what about : $thecustid = "SELECT custid FROM customers WHERE userid ='" $_SESSION['MM_Username']."'"; |
QUOTE |
On 16-Jun-2005, "Kimmo Laine" <[Email Removed] wrote: <... Actually you need to remove the single quotes from around MM_Username (or add {} braces around the whole variable), too. If userid is not numeric, you need to add single quotes around its value to make the SQL correct, so you get: $thecustid = "SELECT custid FROM customers WHERE userid = '$_SESSION[MM_Username]' "; |
QUOTE |
http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing |
QUOTE |
aggelos wrote: what about : $thecustid = "SELECT custid FROM customers WHERE userid ='" $_SESSION['MM_Username']."'"; This is also acceptable and might even be more efficient than parsing variables within a string. To make it more efficient, replace double quotes (") with single (') when you don't need them ie. when there are no variables inside the string. |