Help - Search - Member List - Calendar
Full Version: correct syntax?
WorkTheWeb Forums > Webmaster Resources > PHP Help
Support our Sponsors!
toedipper
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.

Kimmo Laine
"toedipper" <[Email Removed]> kirjoitti
viestiss:3Gnse.18341$[Email Removed]...
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']';


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.

--
"I am pro death penalty. That way people learn
their lesson for the next time." -- Britney Spears

[Email Removed]

Tom Thackrey
On 16-Jun-2005, "Kimmo Laine" <[Email Removed]> wrote:

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.

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]' ";

http://www.php.net/manual/en/language.type....string.parsing

--
Tom Thackrey
www.creative-light.com
tom (at) creative (dash) light (dot) com
do NOT send email to [Email Removed] (it's reserved for spammers)

Veikko Mkinen
Tom Thackrey wrote:
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]' ";

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.

QUOTE
http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing


-veikko

--
veikko
mail@ .com
makinen

aggelos
Veikko Mäkinen wrote:
what about :
$thecustid = "SELECT custid FROM customers WHERE userid ='"
$_SESSION['MM_Username']."'";

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


--
Love Peace and Linux

Veikko Mäkinen
aggelos wrote:

QUOTE
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.


-veikko

P.S. Please learn how to quote and reply with out top-posting. thanks.

--
veikko
mail@ .com
makinen

Tony
Tom Thackrey wrote:
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]' ";

That would match userid to the literal value '$_SESSION[MM_Username]', not
to the value of the variable $_SESSION[MM_Username]

Also, removing the quotes inside the brackets won't work if you want the
value of the Session variable "MM_Username" (or any other array variable).
You need to close quotes & concatenate:

$thecustid = "SELECT custid FROM customers WHERE userid = '" .
$_SESSION[MM_Username] . "'";
Note the single-quote followed by a double-quote after =, and the last set
is double-quote single-quote double-quote, to close the quote to the query.

Alternately, you could assign $_SESSION[MM_Username] to a simple (non-array)
variable that is included in the quoted string:

$username = $_SESSION[MM_Username];
$thecustid = "SELECT custid FROM customers WHERE userid = '$username'";

since a non-array variable will translate properly in this case.

QUOTE
http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing

Exactly.

--
Tony Garcia
Web Right! Development
[Email Removed]

Tony
Veikko Mkinen wrote:
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.

In this case, the double-quotes are necessary as the single-quotes are
needed to delimit the value of the string in the SQL query. Alternately, you
could escape the single quotes, but using double-quotes I think makes more
sense in this case - at the very least, it makes for more readable code.

--
Tony Garcia
Web Right! Development
[Email Removed]


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.