简体   繁体   中英

Ignore mysql_query SELECT

I'm using fopen and fwrite to create a new php file, but it's returning an error at the line of a query, specifically on 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);

?>

I know that the backslash escape the variable, but how can i avoid the execution of the "SELECT" to write it at the file.

Thanks!

只需使用如下单引号:

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

Just add backslash to escape double quotes too.

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

I use this technique more and more now, maybe you will like that too:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM