简体   繁体   中英

Basic SQL syntax, MAMP, PHP & MySQL

I know this is simple but it's really annoying,

Why does this work:

    $query = $pdo->prepare('SELECT index FROM active_notifications WHERE direction = ">"');

and this works:

    $query = $pdo->prepare('SELECT trigger_price FROM active_notifications WHERE direction = ">"');

But this wont!:

    $query = $pdo->prepare('SELECT trigger_price, index FROM active_notifications WHERE direction = ">"');

I get this rubbish:

Could not connect to the database. Reason: exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;

You should not use reserved words like 'index' as the names of your columns (or tables, or schemas, or anything that requires an identifier).

In all likelihood, your database is able to infer 'index' as the name of the column from the context of the first statement, but selecting multiple columns causes it to be ambiguous.

Regardless, that's not the real issue. You don't want to use reserved words like 'index' as identifiers or else weird things will happen. Try changing the name of your column.

You can refer to MySQL reserved words by escaping them with ` in your SQL statement, ie:

SELECT trigger_price, 
`index`
FROM active_notifications 
WHERE direction = ">"

Read the MySQL docs for more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM