忽略mysql_query SELECT

时间:2015-01-21 00:31:30

标签: php

我使用fopen和fwrite创建一个新的php文件,但它在查询行返回错误,特别是在SELECT上:

<?php
# set $defaultDir, $fileName and $userID

$fileContent = "<?php
\$getBlogPostQ = mysql_query("SELECT name FROM posts WHERE id_users='".\$userID."' LIMIT 1");
?>";

$createFile = fopen($defaultDir . "/" . $fileName . ".php","a");
fwrite($createFile,$fileContent);
fclose($createFile);

?>

我知道反斜杠会转义变量,但是如何避免执行&#34; SELECT&#34;把它写在文件上。

谢谢!

3 个答案:

答案 0 :(得分:2)

只需使用这样的单引号:

$fileContent = '<?php $getBlogPostQ = mysql_query("SELECT name FROM posts WHERE id_users=\'' . $userID . '\' LIMIT 1"); ?>';

答案 1 :(得分:0)

只需添加反斜杠即可转义双引号。

$fileContent = "<?php
\$getBlogPostQ = mysql_query(\"SELECT name FROM posts WHERE id_users='\".\$userID.\"' LIMIT 1\");
?>";

答案 2 :(得分:0)

我现在越来越多地使用这种技术,也许你也会喜欢这样:

$fileContent = <<<ANYTHINGYOUWANT
<?php
 $getBlogPostQ = mysql_query("SELECT name FROM posts WHERE id_users='".$userID."' LIMIT 1");
?>
ANYTHINGYOUWANT;