Google my name...
- Joined
- Nov 9, 2011
- Messages
- 483
- Reaction score
- 151
Hi everyone,
Recently a few threads have popped up on the help section in regards to MySQL syntax being incorrect. This is a quick guide on why this happens and how to fix it.
So firstly we are going to pretend this is our query:
So firstly any commercial instructor would (should) tell you that command words must be capitalized. Although this is not a syntax issue it makes reading the query much easier!
Our new query:
Okay so this query would error due to "character" being a command and not a field.
To safely escape any issues like this all fields/table names should be escaped using the ` (tildes) do not confuse this with an apostrophe.
So our new query looks like this:
Now this query would run fine with no issues but even then the syntax is not commercially viable.
The final step is to place apostrophes around any values, in this case 21.
Now this is a MySQL query that any decent company would teach to their employees while programming, whether it be C#, PHP or Java.
I hope this might clear up some stuff, please note this syntax stands for queries no matter how complex.
Edit:
Since we have joined the discussion further than syntax this comment is a very good one.
All the best,
Richard Komakech.
Recently a few threads have popped up on the help section in regards to MySQL syntax being incorrect. This is a quick guide on why this happens and how to fix it.
So firstly we are going to pretend this is our query:
Code:
select * from users where character = 21;
So firstly any commercial instructor would (should) tell you that command words must be capitalized. Although this is not a syntax issue it makes reading the query much easier!
Our new query:
Code:
SELECT * FROM users WHERE character = 21;
Okay so this query would error due to "character" being a command and not a field.
To safely escape any issues like this all fields/table names should be escaped using the ` (tildes) do not confuse this with an apostrophe.
So our new query looks like this:
Code:
SELECT * FROM `users` WHERE `character` = 21;
Now this query would run fine with no issues but even then the syntax is not commercially viable.
The final step is to place apostrophes around any values, in this case 21.
Code:
SELECT * FROM `users` WHERE `character` = '21';
Now this is a MySQL query that any decent company would teach to their employees while programming, whether it be C#, PHP or Java.
I hope this might clear up some stuff, please note this syntax stands for queries no matter how complex.
Edit:
I'd like to make it a point that when using PDO to run mysql queries with PHP, do not surround the variable with apostrophes. I did this and it bugged out because of that.
This bugs out when binding :var with PDO. So make sure not to put apostrophes around :var, as such:PHP:$statement->prepare("SELECT * FROM `users` WHERE `character` = ':var'");
PHP:$statement->prepare("SELECT * FROM `users` WHERE `character` = :var");
Since we have joined the discussion further than syntax this comment is a very good one.
All the best,
Richard Komakech.
Last edited: