gaza165 Posted December 18, 2008 Share Posted December 18, 2008 i am pulling back a record from the database on every click of the submit button... what i want to do is write the data im pulling from the database into a text file. the code i have just overwrites the previous data. i want it to keep adding on to it like below. { "id": "10","nick":"Garry","message":"yeah good mate you?","time":"0.00104808807373"} { "id": "11","nick":"Dan","message":"yo!","time":"0.00115013122559"} this is the code i am using. <?php $data = array(); while ($row = mysql_fetch_array($result)) { $data[] ='{ "id": "'.htmlentities($row['chat_id']).'","nick":"'.htmlentities($row['nick']).'","message":"'.htmlentities($row['message']).'","time":"'.$time.'"}'; $fp = fopen('chat.txt', 'w'); fwrite($fp, $data[0]); } $json = '{"latest":"'.$lastid.'","response":['; $json .= implode(',',$data); $json .= ']}'; echo $json; ?> Link to comment https://forums.phpfreaks.com/topic/137540-fwrite-question/ Share on other sites More sharing options...
JonnoTheDev Posted December 18, 2008 Share Posted December 18, 2008 wrong parameter for fopen() should be: fopen("chat.txt", "a"); fopen parameters: http://uk3.php.net/manual/en/function.fopen.php Link to comment https://forums.phpfreaks.com/topic/137540-fwrite-question/#findComment-718793 Share on other sites More sharing options...
dennismonsewicz Posted December 18, 2008 Share Posted December 18, 2008 change this $fp = fopen('chat.txt', 'w'); to this $fp = fopen('chat.txt', 'a'); The a stands for append.. so the data just keeps adding onto whatever else is there Link to comment https://forums.phpfreaks.com/topic/137540-fwrite-question/#findComment-718797 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.