catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.83k stars 1.15k forks source link

Why Mysql table name use Double quotes? #292

Closed lxepoo closed 6 years ago

lxepoo commented 8 years ago

hi~

// Make MySQL using standard quoted identifier
$commands[] = 'SET SQL_MODE=ANSI_QUOTES';

No effect, in version 1.0

so , Generated SQL statement:

SELECT * FROM "yc_category" LIMIT 1

error in mysql version 5.6.13

How to solve?

Thank you!

SyuTingSong commented 8 years ago

What error?

ghost commented 8 years ago

Same problem here. If I try use the generated sql query with quotation mark around table name. I get in error.

The "SET SQL_MODE=ANSI_QUOTES" when using MySQL appears not working because i get Syntax error and no result on my inserts.

omriamos commented 8 years ago

Same problem here. I don't understand why on earth are you using the non-standard double quotes for identifiers, instead of backticks. THIS IS PLAIN WRONG. Setting SQL_MODE = ANSI_QUOTES is bad practice, and more important - on some server configurations simply doesn't work at all.

I had to manually change some code in the medoo.php file (some 'x_quote' functions) in order to fix this, but I can't be sure I won't get side effects from this change...

lxepoo commented 8 years ago

thx !

for closed

omriamos commented 8 years ago

why did you close this?

lxepoo commented 8 years ago

For a long time, I have forgotten why I asked. OK, I'm already open. Hah

lxepoo commented 8 years ago

The old version of the SQL statement generated must be combined with "SET SQL_MODE=ANSI_QUOTES", otherwise the error will be prompted, although this is compatible with SQL to Server and Mysql, but I still hope to get a better deal, such as the delimiter as variables? Different types of separators are then used to determine the different types of separators. I am a non English speaking country developer, which is translated by Google.

omriamos commented 8 years ago

Just a quick update, I've found out why (at least in my case) it didn't work. The SET SQL_MODE=ANSI_QUOTES query is running at the very beginning when the connection is made, but it's important to understand that this is a sessional variable. Sometimes, a long-running PHP script can actually make the mysql connection timeout, so if the script try to run another query after it finishes with the long-running process - the mysql server will take this as a new session and the variable will be reset to default.

lxepoo commented 8 years ago

There is an end to the session, once the failure to open the correct "SQL_MODE=ANSI_QUOTES SET", it is likely to be wrong. Refer to my suggestion, judge the database type, and enforce the correct symbol.

costafrancesco94 commented 7 years ago

Hi :) Sorry if I intromit... i'm starting using this library in some of my projects, and I incurred in the same error in quoting things in general... I debugged and corrected it for my use case and now the (not so many) query i need, works. I don't know about your best practice about suggest coding, if you want me to pull a request just tell me... I don't know if this modifications can be acceptable but i just replaced the "column" with the ` symbol (i cannot reproduce it in comments xD) in the place they fixed my queries.

Modifications in Version 1.2.1 src.zip (Pay attention the attacched file have the debug parameter set to true, before you're going crazy...)

catfan commented 6 years ago

v1.5 will output by the database default quote identifier.

https://medoo.in/api/whatsnew

omriamos commented 6 years ago

WHY DID YOU CLOSE THIS TOPIC (AGAIN), if the issue is still not fixed??

Keep in mind that, this is only output for debugging the query. Medoo is actually using standard quote identifier for internal execution for all database.

catfan commented 6 years ago

@omriamos It don't need to be fixed, it's now a bug. And it also output with the database default quote identifier for debug().

omriamos commented 6 years ago

It IS a bug, on a default server configuration, without explicitly adding SET SQL_MODE=ANSI_QUOTES to every session, MySQL will die with an error message:

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 '"User"' at line 1