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?

1 Answer
1

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.com/questions/23129/benefits-of-using-backtick-in-mysql-queries

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *