Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why did I have to use backticks around the table name in my MySQL query?

As far as I have read, backticks are not required around, for example, table names in queries: ("INSERT INTO table_name...."). In addition, to my knowledge, underscores are perfectly acceptable syntax to use in names: foo_bar. My question then is, why did this query fail:

mysql_query("INSERT INTO quick_links WHERE ...etc");

when this query worked perfectly:

mysql_query("INSERT INTO `quick_links` WHERE ...etc");

Are "quick" or "links" reserved words??? I didn't think so, but then again, I'm fairly new to MySQL, so I apologize. Also,is using backticks necessary in certain "Storage Engines" (referencing phpMyAdmin for the terminology)? I happen to be using "InnoDB" if that makes any difference.

Thanks for the answers, I'm new to MySQL, and I like to make sure I understand nuances like these.

My full query was this:

mysql_query("INSERT INTO `quick_links` VALUES (
    '$user_id', '$ql_name', '$ql_url', '$ql_img'
)"); 

$ql_url and $ql_name were obtained via $_POST and then sanitized with trim and mysql_real_escape_string before being used in the query. $ql_img is simply a url referring to a directory in which a thumbnail is uploaded, and $user_id is there to, well, identify the particular user who is logged in. Both queries were exactly the same, literally, except for the backticks around the table_name.

like image 321
John Zimmerman Avatar asked Jan 15 '23 10:01

John Zimmerman


1 Answers

Backtick are only used to escape MySQL Reserved words. They are optional if didn't use any of the reserved keywords provided that the tableNames or columnNames used has no spaces on it.

This page tells the allowable characters to be used in schema.

like image 130
John Woo Avatar answered Apr 30 '23 12:04

John Woo