0

I'm trying to construct a query for a cron that will run.

mysql_query("UPDATE `stocks` SET price='$pricez', open='$openz', high='$highz', low='$lowz', change='$changez', time='$times', percent='$percentz' WHERE symbol = '$symbolz' "); 

The error I get is

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'change='-0.10', time='1406050151', percent='-0.35%' WHERE symbol = 'ALMB.CO'' at line 1

Scavenged SOF and have yet to find a solution.

1
  • 1
    Any reserved words being used? yep.. change wrap change in back ticks change Commented Jul 22, 2014 at 17:32

2 Answers 2

2

Reserved words just bit you: Change is a reserved word thus needs to be escaped: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

mysql_query("UPDATE `stocks` SET price='$pricez', open='$openz', high='$highz', 
low='$lowz', `change`='$changez', time='$times', percent='$percentz' WHERE symbol = '$symbolz' ");

So what is a reserved word?

They are words the engine uses to interpert specific requested commands. When these words are used as identifiers for tables or columns they must be treated in a specific manner usually escaping the words for the RDBMS involved.

Sign up to request clarification or add additional context in comments.

Comments

2

Looks like you are using some reserved words for column names: Change and Time You can escape these with backticks (`), or choose new coumn names

UPDATE `stocks` 
SET     `price`='$pricez', 
        `open`='$openz', 
        `high`='$highz', 
        `low`='$lowz', 
        `change`='$changez', 
        `time`='$times', 
        `percent`='$percentz' 
WHERE symbol = '$symbolz' 

5 Comments

dev.mysql.com/doc/refman/5.5/en/reserved-words.html Open, time and percent don't appear on reserved words list for MySQL. Change does however.
Sorry was thinking T-SQL. Time is definitely on the list.
I like the edit with everything in backticks, that's the way to go there's no doubt then +1
I lied Time is a keyword but can be used without backticks. When In doubt escape everything!
@andrew while it does reduce the risk, it obfuscates the underlying problem to future maintenance. I'd much rather see it used only when needed; and only when it's too late to change the column name.

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.