最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Backticks (`) Instead of Single Quotes (') in an SQL Statement?

programmeradmin1浏览0评论

I have this code which is partially shown here for brevity:

global $wpdb;
// creates jwp_bids in database if not exists
$table = $wpdb->prefix . "jwp_bids"; 
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table (
    `id` mediumint(9) NOT NULL AUTO_INCREMENT,
    `bid_amt` text NOT NULL,

When I originally typed the code, I used ' within the parentheses and the code would not run.

I then changed the Single Quote to a Backtick and the code ran perfectly.

Would someone please explain:

  • What is the proper term for the oddball quotes :)
  • Why are they used instead of ' ?
  • Is this only pertinent to MySQL? WP? Php?
  • Where can I find a technical reference for this sort of thing so I don't have to bug you fine folk?

I have this code which is partially shown here for brevity:

global $wpdb;
// creates jwp_bids in database if not exists
$table = $wpdb->prefix . "jwp_bids"; 
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table (
    `id` mediumint(9) NOT NULL AUTO_INCREMENT,
    `bid_amt` text NOT NULL,

When I originally typed the code, I used ' within the parentheses and the code would not run.

I then changed the Single Quote to a Backtick and the code ran perfectly.

Would someone please explain:

  • What is the proper term for the oddball quotes :)
  • Why are they used instead of ' ?
  • Is this only pertinent to MySQL? WP? Php?
  • Where can I find a technical reference for this sort of thing so I don't have to bug you fine folk?
Share Improve this question edited Jan 5, 2017 at 10:01 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Jan 4, 2017 at 22:33 JamesJames 3513 gold badges4 silver badges11 bronze badges 3
  • 1 What happens if you leave out the backticks entirely, does it still run? I'm pretty sure this is a MySQL nuance and it has to do with interpreting column names as Strings ( when using single quotes ) instead of identifying them as columns ( using backticks or nothing at all ). – Howdy_McGee Commented Jan 4, 2017 at 22:40
  • I'll give it a test to see. What's the best reference resource out there for MySQL where such things are illuminated? – James Commented Jan 4, 2017 at 22:55
  • Okay so it does work WITHOUT backticks. Technical reference that is good for such details? – James Commented Jan 4, 2017 at 23:49
Add a comment  | 

1 Answer 1

Reset to default 8

Backticks and regular quotes (both single and double) have distinct meanings in SQL. Quotes indicate a literal string, whereas backticks are quoted identifiers. This isn't specific to WordPress, but rather a general way in SQL of quoting columns or tables.

For example, imagine you're running a query comparing two columns:

SELECT * FROM wp_posts WHERE post_name = post_title

If you compare with single quotes ('post_name' = 'post_title') this would compare the literal strings, which are never equal. However, with backticks, it would refer to the column.

Why use backticks at all? If a column is named the same as a reserved word in SQL, or contains a space, you need to quote it. For example, if you had a column called count, you'd need to quote that every time you refer to it, as COUNT() is a function (and hence, reserved word) in MySQL.

See also https://dba.stackexchange/questions/23129/benefits-of-using-backtick-in-mysql-queries

发布评论

评论列表(0)

  1. 暂无评论