SQL – Single quotes and backticks for query
-
I have referred to Stack Overflow, sorry because it might be a stupid question because I’m learning SQL.
Let’s say I have the below code:
function get_data ( $name ) { global $wpdb; $wpdb -> get_var ( $wpdb -> prepare ( "SELECT value FROM $table_name WHERE name = '$name'" ) ); }1) May I know why I have to use the single quotes around the
$name? Wouldn’t it become a string that prints$name? Like the below:$test = 'Hii'; echo "My friend, '$test'";Which prints
My friend, $test, instead it should be:$test = 'Hii'; echo "My friend, $test";Which prints
My friend, Hii.
2) For good practice, should I use backticks for table and column names always like the below ( I don’t know how to escape the backtick here, I’ll imagine the backtick as ! ):
$wpdb -> prepare ( "SELECT !value! FROM $table_name WHERE !name!= '$name'" )
The topic ‘SQL – Single quotes and backticks for query’ is closed to new replies.