基本上我试着让php文件创建另一个php文件,里面有XHTML,PHP和MYSQL。我试图包含一个MYSQL查询,但问题是我用来启动fwrite字符串的撇号(')在到达MYSQL查询时会感到困惑,因为它也有一个撇号,但我只是希望该字符串不继续结束。我该怎么做才能工作?提前感谢您的帮助。
代码(只是相关位):
$filename = "websites/".$firstpage.".php";
$filehandle = fopen($filename, 'w') or die("error opening file");
$datawrite = '
$query = "SELECT websitetitle FROM $weblist WHERE webID='$webID'";
$result = mysql_query ($query);
$row = mysql_fetch_array( $result );
echo $row['webID'];
';
fwrite($filehandle, $datawrite);
fclose($filehandle);
答案 0 :(得分:1)
你应该在'查询中'转义''这不会执行并结束字符串
$filename = "websites/".$firstpage.".php";
$filehandle = fopen($filename, 'w') or die("error opening file");
$datawrite = '
$query = "SELECT websitetitle FROM $weblist WHERE webID=\'$webID\'";
$result = mysql_query ($query);
$row = mysql_fetch_array( $result );
echo $row[\'webID\'];
';
fwrite($filehandle, $datawrite);
fclose($filehandle);
请查看此内容以供参考 http://php.net/manual/en/language.types.string.php
答案 1 :(得分:0)
只需使用正确的转义并在撇号之前放置\
。
$datawrite = '
$query = "SELECT websitetitle FROM $weblist WHERE webID=\'$webID\'";
$result = mysql_query ($query);
$row = mysql_fetch_array( $result );
echo $row[\'webID\'];
';