我试图使用php创建一个文本文件,这个值是通过db调用来实现的。
$result = mysql_query("SELECT code FROM topic_master");
while ($row = mysql_fetch_array($result)) {
$x1=$row{'code'};
exec("printf $x1 >> code.txt");
}但是该值没有插入到code.txt中。只在code.txt中插入数组(代码)。实际上,$row{'code'}有"#xy {} () %“。如何将值写入txt文件。
发布于 2013-09-05 17:55:39
首先,您不应该使用$row{'code'}正确的用法是$row'code'
此外,我建议使用file_put_contents()而不是exec,以便您可以这样做。
$result = mysql_query("SELECT code FROM topic_master");
while ($row = mysql_fetch_array($result)) {
$x1=$row['code'];
file_put_contents('code.txt',$x1, FILE_APPEND);
}发布于 2013-09-05 17:58:01
这将解决您的问题:
$result = mysql_query("SELECT code FROM topic_master");
while ($row = mysql_fetch_array($result)) {
file_put_contents('code.txt',$row['code'],FILE_APPEND);//FILE_APPEND is necessary
}发布于 2013-09-05 17:54:38
用目录代替那个主管然后做
$s = "";
while ($row = mysql_fetch_array($result)) {
$x1=$row['code'];
$s .= $x1 . "\n";
}
file_put_contents("code.txt", $s, FILE_APPEND);https://stackoverflow.com/questions/18642836
复制相似问题