我想将变量$ rows的内容写入文件out.txt。
变量$ rows放在while循环中,可以由从数据库中检索的几个不同的行构成。
不幸的是,下面的代码只写了预期输出的一行(最后一行)。
$myFile = "/var/www/test/out.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$content = $rows['text'];
fwrite($fh, $content);
fclose($fh);
这是PHP代码(正在运行),它允许我在浏览器中查看$ rows变量的内容。
<?
if(isset($_POST['type'], $_POST['topic'], $_POST['tense'], $_POST['polarity']))
{
$tipo = $_POST['type'];
$argomento = $_POST['topic'];
$tempo = $_POST['tense'];
$segno = $_POST['polarity'];
foreach ($tipo as $key_tipo => $value_tipo)
{
foreach ($argomento as $key_argomento => $value_argomento)
{
foreach ($tempo as $key_tempo => $value_tempo)
{
foreach ($segno as $key_segno => $value_segno)
{
$q_tip_arg_tem_seg = "SELECT * FROM entries WHERE type = '".$value_tipo."' AND topic = '".$value_argomento."' AND tense = '".$value_tempo."' AND polarity = '".$value_segno."'";
$qu_tip_arg_tem_seg = mysql_query($q_tip_arg_tem_seg);
while ($rows = mysql_fetch_array($qu_tip_arg_tem_seg))
{
echo $rows['type']."<br />";
}
}
}
}
}
}
else
{
echo "You forgot to check something."."<br>"."Please select at least one type, topic, tense and polarity value."."<br>";
}
?>
你能救我吗?
感谢。
答案 0 :(得分:2)
您只能在文件中写出一行。你可能想要这样的东西:
$myFile = "/var/www/test/out.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
if ($fh) {
while ($row = getrow()) {
$content = $rows['text'];
fwrite($fh, $content); // perhaps add a newline?
}
fclose($fh);
}
因此,打开文件,将所有内容写入其中,然后关闭它。
答案 1 :(得分:0)
你可以用两种方式做到这一点
1.首先从out.txt文件中获取详细信息,然后将其与$ row连接。如果你已经在out.txt中显示了内容,还有$ row text.finaly用内容写入文件。
2.将单个变量中的所有行合并到一个varialble中,然后进行文件写操作。
答案 2 :(得分:0)
如果您只想查看数组的外观,请尝试使用print_r()以一种易于阅读的格式打印数组。
如果你想要一个更结构化的布局,这可能还不够。
$myFile = "/var/www/test/out.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, print_r($rows,TRUE) );
fclose($fh);