Web Analytics Made Easy -
StatCounter Whats wrong with this line? - CodingForum

Announcement

Collapse
No announcement yet.

Whats wrong with this line?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Whats wrong with this line?

    What an Earth is wrong with this code?

    Code:
    $query = "select sizes from products where id='$id'"; $result = mysql_query($query); $row = mysql_fetch_array($result); if ($row == 0) {echo "";} else {echo "Small - $row['sizes']";}
    PHP Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
    Last edited by Democrazy; Sep 12, 2011, 10:54 AM.

  • #2
    $row should have been $row['sizes']

    Comment


    • #3
      else {echo "Small - $row['sizes']";}

      Remove those single quote marks so it looks like this: $row[sizes]

      Also see the link in my signature about quotes to learn about using them properly. When using an array and its key inside double quotes you don't use the single quotes.
      "Tango says double quotes with a single ( ' ) quote in the middle"
      '$Name says single quotes with a double ( " ) quote in the middle'
      "Tango says double quotes ( \" ) must escape a double quote"
      '$Name single quotes ( \' ) must escape a single quote'

      Comment


      • #4
        Originally posted by tangoforce View Post
        else {echo "Small - $row['sizes']";}

        Remove those single quote marks so it looks like this: $row[sizes]
        PHP Code:
        // Or
        else { echo "Small - {$row['sizes']}"; }
        // Or
        else { echo 'Small - ' $row['sizes']; } 
        It's best practice not to omit the quotes when referencing array keys.

        Comment

        Working...
        X
        😀
        🥰
        🤢
        😎
        😡
        👍
        👎