写入CSV问题

时间:2013-08-10 06:34:35

标签: php fwrite

输出文件似乎没有正确地将数据放入CSV

include_once ('database_connection.php');//Including our DB Connection file
if(isset($_GET['keyword'])){//IF the url contains the parameter "keyword"
 $keyword =     trim($_GET['keyword']) ;//Remove any extra  space
$keyword = mysqli_real_escape_string($dbc, $keyword);//Some validation

$query = "select topictitle,topicdescription from topics where topictitle like '%$keyword%' or topicdescription like '%$keyword%'";
//The SQL Query that will search for the word typed by the user .

$result = mysqli_query($dbc,$query);//Run the Query
if($result){//If query successfull
 if(mysqli_affected_rows($dbc)!=0){//and if atleast one record is found
 while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){ //Display the record
 echo '<p> <b>'.$row['topictitle'].'</b> '.$row['topicdescription'].'</p>
 <input type="text" value="'.$_GET['keyword'].'" /><input type="text" value="'.$row['topicdescription'].'" />
 '   ;

                $numbre = fopen($_GET['keyword'].'.csv',"w");
                echo fwrite($numbre, "topictitle,topicdescription\r\n");
                echo fwrite($numbre, $_GET['topictitle'].",&".$_GET['topicdescription']);
                fclose($numbre);

 }
 }else {
 echo 'No Results for :"'.$_GET['keyword'].'"';//No Match found in the Database
 }

}
}else {
 echo 'Parameter Missing in the URL';//If URL is invalid
}

输出文件放置除topicdescription之外的所有内容。

2 个答案:

答案 0 :(得分:1)

而不是这一行:

echo fwrite($numbre, "topictitle,topicdescription\r\n");

使用此

echo fwrite($numbre, $row['topictitle'] . "," . $row['topicdescription'] . "\r\n");

同时将$_GET['topictitle']$_GET['topicdescription']更改为$row['topictitle']$row['topicdescription']

答案 1 :(得分:0)

在行echo fwrite($numbre, $_GET['topictitle'].",&".$_GET['topicdescription']);中,您的意思是将数据库查询中的$row["topictitle"]$row["topicdescription"]写入CSV而不是URL参数吗?