用PHP将双引号放在mysql字符串中

用PHP将双引号放在mysql字符串中

问题描述:

I have this code in PHP:

$_pagi_sql='SELECT * FROM programas WHERE MATCH(titulo, texto,pais) AGAINST ('"$criterio"' IN BOOLEAN MODE) AND operador=34 And validez >= NOW()';

I want to put exactly this string in the variable $_pagi_sql:

SELECT * FROM programas WHERE MATCH(titulo, texto,pais) AGAINST ('"STH"' IN BOOLEAN MODE) AND operador=34 And validez >= NOW()

However the double quotes are not properly stored in the variable $_pagi_sql

How can it be done?

You can enter double quotes by adding slashes before your single quotes.

$_pagi_sql='SELECT * FROM programas WHERE MATCH(titulo, texto,pais) AGAINST (\'"$criterio"\' IN BOOLEAN MODE) AND operador=34 And validez >= NOW()';

Replace the single quotes from right handle side with double quotes:

$_pagi_sql="SELECT * FROM programas WHERE MATCH(titulo, texto,pais) AGAINST ('\"$criterio\"' IN BOOLEAN MODE) AND operador=34 And validez >= NOW()";

Or, get rid of the quotes, use heredoc:

$_pagi_sql = <<<SQL
SELECT * FROM programas WHERE MATCH(titulo, texto,pais) AGAINST ('"$criterio"' IN BOOLEAN MODE) AND operador=34 And validez >= NOW()
SQL;