0

This code keeps erroring.

Error Message: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/soulz/public_html/inbox.php on line 19

Here is the code:

mysql_query("UPDATE `messages` SET message_title = '[NO SUBJECT]' WHERE `message_id`=$row['message_id']");

3 Answers 3

3

Use curly braces:

mysql_query("UPDATE `messages` SET message_title = '[NO SUBJECT]' 
             WHERE `message_id`={$row['message_id']}");
Sign up to request clarification or add additional context in comments.

4 Comments

Hehe, so much for the "more elaborate syntax" :-) +1
This propably works. But how "nice" can it be, if we have trouble reading such a simple piece of code? $messageId = (int)$row['message_id']; --> use $messageId var in SQL... or merge SQL and $row['message_id'] by using sprintf();
@HBublitz I have no trouble at all reading this. In fact, I find it more readable than using sprintf()
Excuse me, you're obvoiusly used to that way and I don't doubt you're working fine with it. Nevertheless I would like to recommend always assuming more than one solution - especially when someone is not used to any of them yet. I prefer clear assignments and strings without functionality.
2

Don't put apostrophes around the field name:

mysql_query("UPDATE `messages` SET message_title = '[NO SUBJECT]'
             WHERE `message_id`=$row[message_id]");
                                     ^^^^^^^^^^

Inside quoted strings, you cannot use additional quotation marks for array field names. There's an alternative, more elaborate syntax involving braces if you have a very complicated array expression, but you don't need that here.

2 Comments

This strikes me as a bit odd; they don't recommend you to write $foo[bar] but inside quoted strings it's OK. Hmmm...
There's no choice: Outside strings you have to write $foo["bar"] or $foo['bar'], inside strings you must not (except for inside the braces).
0

It seems message_id is integer, so you can fix that error with a best practice.

mysql_query("UPDATE `messages` SET message_title = '[NO SUBJECT]'
             WHERE `message_id`=" . intval($row['message_id']));

You can use strval() for strings. Both functions are detailed in intval() manual page and strval() manual page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.