Professional Web Applications Themes

correct syntax? - PHP Development

Hello, Is this the right syntax for assigning a sql selct to a variable. When I use this I get a p error on the line that this is on. $thecustid = 'SELECT custid FROM customers WHERE userid = $_SESSION['MM_Username']'; Thanks, td....

  1. #1

    Default correct syntax?

    Hello,

    Is this the right syntax for assigning a sql selct to a variable. When
    I use this I get a p error on the line that this is on.

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

    Thanks,


    td.
    toedipper Guest

  2. #2

    Default Re: correct syntax?

    "toedipper" <com> kirjoitti
    viestissä:3Gnse.18341$ntli.net... 


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

    com


    Kimmo Guest

  3. #3

    Default Re: correct syntax?


    On 16-Jun-2005, "Kimmo Laine" <com> wrote:
     
    >
    >
    > 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 ape. 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.[/ref]

    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.types.string.php#language.types.string.pa rsing

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

  4. #4

    Default Re: correct syntax?

    Tom Thackrey wrote: 
    >>
    >>
    >>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 ape. 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.[/ref]
    >
    >
    > 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]' ";[/ref]

    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.
     


    -veikko

    --
    veikko
    mail .com
    makinen
    Veikko Guest

  5. #5

    Default Re: correct syntax?

    Veikko Mäkinen wrote:
    what about :
    $thecustid = "SELECT custid FROM customers WHERE userid ='"
    $_SESSION['MM_Username']."'";
     
    >>
    >>
    >> 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]' ";[/ref]
    >
    > 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.
    > [/ref]
    http://www.php.net/manual/en/language.types.string.php#language.types.string.pa rsing 

    --
    Love Peace and Linux
    aggelos Guest

  6. #6

    Default Re: correct syntax?

    aggelos wrote:
     

    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
    Veikko Guest

  7. #7

    Default Re: correct syntax?

    Tom Thackrey wrote: 
    <...> 

    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.
     

    Exactly.

    --
    Tony Garcia
    Web Right! Development
    com


    Tony Guest

  8. #8

    Default Re: correct syntax?

    Veikko Mäkinen wrote: 
    >
    > 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.[/ref]

    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
    com


    Tony Guest

Similar Threads

  1. Correct syntax for a pop up window
    By martinharvey in forum ASP.NET Data Grid Control
    Replies: 2
    Last Post: January 8th, 09:29 AM
  2. Replies: 3
    Last Post: January 18th, 10:54 PM
  3. Need help on the correct SQL syntax
    By miyaki in forum Informix
    Replies: 2
    Last Post: December 17th, 07:06 PM
  4. Correct syntax for bzip and gnu tar
    By LHradowy in forum UNIX Programming
    Replies: 5
    Last Post: October 8th, 10:35 PM
  5. Replies: 2
    Last Post: August 1st, 06:34 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139