zeeshan_haider000 Posted February 28, 2009 Share Posted February 28, 2009 Hi, Is it possible to store database results from a mysql_query into a variable/array? and then write it to a file? EX: $r = mysql_query(SOME QUERY); $varx = all the results of $r; .... fwrite (FILE, $varx); ... ? Quote Link to comment https://forums.phpfreaks.com/topic/147350-php-question/ Share on other sites More sharing options...
cooldude832 Posted February 28, 2009 Share Posted February 28, 2009 You can but why? You need to also use mysql_fetch_assoc() or a similar function to get data. The actual resources are no good to store because they die when your connection closes (on page end usually) Quote Link to comment https://forums.phpfreaks.com/topic/147350-php-question/#findComment-773442 Share on other sites More sharing options...
zeeshan_haider000 Posted February 28, 2009 Author Share Posted February 28, 2009 You can but why? You need to also use mysql_fetch_assoc() or a similar function to get data. The actual resources are no good to store because they die when your connection closes (on page end usually) I want to store the data into a, say a text file... Like first run the query, and then use the while loop: while ($r = mysql_fetch_array($b)){ display data; } and then make a copy of this whole data (ex: while loop's data). fwrite($fp, All the while loop data)... is this possible? Quote Link to comment https://forums.phpfreaks.com/topic/147350-php-question/#findComment-773445 Share on other sites More sharing options...
Philip Posted February 28, 2009 Share Posted February 28, 2009 $result = mysql_query($query) or die(mysql_error()); // run query $array = array(); // setup array while($row = mysql_fetch_assoc($result)) { $array[] = $row['col1'].' '.$row['col2']; // set whatever variables you are calling, you can also output here while you are at it } $storeInFile = implode('"\n", $array); // implode array to a single string fwrite($fp,$storeInFile); // write to file Something like that? edit: missing a variable, oops Quote Link to comment https://forums.phpfreaks.com/topic/147350-php-question/#findComment-773452 Share on other sites More sharing options...
cooldude832 Posted February 28, 2009 Share Posted February 28, 2009 the whole point of a database is to store info so it doesn't make sense to do what you are doing unless you plan on dumping out to say a csv for export or archive Quote Link to comment https://forums.phpfreaks.com/topic/147350-php-question/#findComment-773468 Share on other sites More sharing options...
zeeshan_haider000 Posted March 1, 2009 Author Share Posted March 1, 2009 $result = mysql_query($query) or die(mysql_error()); // run query $array = array(); // setup array while($row = mysql_fetch_assoc($result)) { $array[] = $row['col1'].' '.$row['col2']; // set whatever variables you are calling, you can also output here while you are at it } $storeInFile = implode('"\n", $array); // implode array to a single string fwrite($fp,$storeInFile); // write to file Something like that? edit: missing a variable, oops thanks, but i found another way that worked, i did something like this: <?php $results = mysql_query($query) or die($er); $c = ""; while ($r = mysql_fetch_assoc($results)){ $c .= 'Data1'; $c .= 'Data2'; $c .= 'ETC'; } $data = $c; $f = fopen($fp, 'w+'); fwrite ($f, $data); fclose ($f); ?> @cooldude, Well i am trying to do something, hard to explain, i hope it works.... Quote Link to comment https://forums.phpfreaks.com/topic/147350-php-question/#findComment-773522 Share on other sites More sharing options...
cooldude832 Posted March 1, 2009 Share Posted March 1, 2009 Try and explain it. Odds are if something in php is "hard" you are doing it wrong Quote Link to comment https://forums.phpfreaks.com/topic/147350-php-question/#findComment-773592 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.