Php quotes with a similar query

I got confused with quotes.

I am trying to make a similar query that looks something like this.

$myQuery = 'SELECT col1 , col2 from my_table WHERE col_value LIKE '%$my_string_variable%'; 

But this query will obviously give an error. What is the correct syntax for this? I tried to mess up the double quotes, but that didn't work either.

+4
source share
6 answers

Combine double and single quotes.

 $myQuery = "SELECT col1 , col2 FROM my_table WHERE col_value LIKE '%$my_string_variable%'"; 

Although I prefer to protect my code from SQL Injection ..

 $myQuery = "SELECT col1 , col2 FROM my_table WHERE col_value LIKE '%" . mysql_real_escape_string($my_string_variable) . "%'"; 
+5
source

Surround everything in double quotes.

 $query = "SELECT col1 , col2 from my_table WHERE col_value LIKE '%$my_string_variable%'"; 
+4
source

You use single quotes inside a single line with delimiters. Try using double quotes to indicate your string.

+1
source
 $myQuery = "SELECT col1 , col2 from my_table WHERE col_value LIKE '%$my_string_variable%'"; 

Need to solve your problem.

+1
source

You use one quote as the beginning of your string and again the single quotes "inside". You can change it to the following:

 $myQuery = "SELECT col1 , col2 from my_table WHERE col_value LIKE '%$my_string_variable%'"; 
+1
source

How about this just in case you don’t want to break the template of your request.

 $myQuery = 'SELECT col1 , col2 from my_table WHERE col_value LIKE "%' .$my_string_variable . '%"'; 

When you use a single quote, do not include the php variable in it, as this single quote treats everything inside it as a value, not a variable.

0
source

Source: https://habr.com/ru/post/1342063/


All Articles