FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Trouble figuring out a parse error





Solugon
Parse error: syntax error, unexpected '{' in /home/solugon/domains/frontlines.frih.net/public_html/phptest/index.php on line 144

Here's the code the error comes from:
Code:

print({row["Name"]}. "'s e-mail address is " . {row["E-mail"]} . "<br>\n");


I've tried removing the {s and }s, using (s and )s instead, putting them in the string itself (with and without backslash escapes for the "s in the brackets), etc... What am I doing wrong?!
n0obie4life
What's the line before that? And before before that?

Code:
print(
{row["Name"]}
. "'s e-mail address is " .
{row["E-mail"]}
. "<br>\n");


And I doubt you use {row["E-mail"]};

It should be..

$row['E-mail']; (if you used mysql_fetch_assoc to get the data from mySQL).
Solugon
Code:

    $db = mysql_connect([host], [username], [password])
    if ($db == FALSE) { print("Yeargh!"); }
    else
    {
        mysql_select_db("solugon_news", $db);

        $query = "SELECT Name, 'E-mail' FROM Newsletter";
        $dbResult = mysql_query($query, $db);

        while ($row = mysql_fetch_array($dbResult))
        {
            print({row["Name"]}. "'s e-mail address is [not!] " . {row["E-mail"]} . "<br>\n");
        }
    }


EDIT: Replaced the info in the connect function for privacy.
Solugon
When I remove the {s and }s, it replaced the "{" in the error with a "[", which is why I'm stumped...
Kaneda
Solugon wrote:
When I remove the {s and }s, it replaced the "{" in the error with a "[", which is why I'm stumped...


That would be two different errors. n0obie basically gave the answer already. This line:

Code:
print({row["Name"]}. "'s e-mail address is [not!] " . {row["E-mail"]} . "<br>\n");


... will give you an error because you shouldn't put brackets around variables, unless they're inside a parsed string (string surrounded by doublequotes), like this:

Code:
echo "My name is $name. Today is {$dayname[$day]}";


In other words, if you have variables which aren't "simple" (I.e., arrays), you use brackets so the parser can tell when the variable reference ends (otherwise it'll stop after "$dayname"), but when they're not inside strings, you shouldn't use brackets.

The second error comes due to this (changed) line:

Code:
print(row["Name"] . "'s e-mail address is [not!] " . row["E-mail"] . "<br>\n");


... because variables should have a $ in front of them. Since row doesn't have a $ in front of it, PHP doesn't recognize it as a variable, and then it gets confused about the array brackets (["Name"]). So, the line should look like this:

Code:
print($row['Name'] . "'s e-mail address is [not!] " . $row['E-mail'] . "<br>\n");


Oh, and you might want to get into the habit of using singlequotes rather than doublequotes for strings - unless you have variables directly embedded in the string (like in the "My name is $name" example) or escape characters (like "\n" for newline). Smile

An alternate way of writing the same would be:

Code:
print("{$row['Name']}'s e-mail address is [not!] {$row['E-mail']}<br>\n");


... I guess that's where you got the brackets around variables from Wink
Solugon
I'm way too used to C++. No $ variable identifiers there. Well, thanks a lot, I'm gonna try this now and see if it works.
Related topics
Help: SQL error after uploading new files(thread updated)
error with cash mod
[PHP] Error link
PHP/Blogger error
Impossible parse error!
Parse error: syntax error, unexpected '{' in /home/frih/publ
php error ?[parse error]
Parse error - need help
PHP Parse error on form handling - New to PHP
Parse error: syntax error, unexpected T_NEW
[PHP] Parse error
Parse Error in PHP I can't figure out.
getting parse error when validating css
Cannot find parse error
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.