grlayouts Posted January 14, 2007 Share Posted January 14, 2007 Ok if i have a database of players. at the end of a round i want to export the top 3 players with only certain stats is this possible?if so how?? Link to comment https://forums.phpfreaks.com/topic/34146-copying-from-database/ Share on other sites More sharing options...
utexas_pjm Posted January 14, 2007 Share Posted January 14, 2007 Your description is a bit vague, are you looking for SQL like this?[code]SELECT stat1, stat2, sta3 FROM `yourtable` ORDER BY stat1 DESC, stat2 DESC, stat3 DESC LIMIT 3[/code] Link to comment https://forums.phpfreaks.com/topic/34146-copying-from-database/#findComment-160640 Share on other sites More sharing options...
grlayouts Posted January 15, 2007 Author Share Posted January 15, 2007 no i want it to Export MYSQL table to a text file using PHP, like get the first 5 records from a table and export them to a text file or php file. Link to comment https://forums.phpfreaks.com/topic/34146-copying-from-database/#findComment-161329 Share on other sites More sharing options...
Hypnos Posted January 15, 2007 Share Posted January 15, 2007 To write a text file, you need to use the fopen and fwrite functions. If it's a PHP file you want to output, is there a reason why you don't want to just use the PHP MySQL functions to get the live data from the database? Link to comment https://forums.phpfreaks.com/topic/34146-copying-from-database/#findComment-161344 Share on other sites More sharing options...
jvrothjr Posted January 15, 2007 Share Posted January 15, 2007 This code will take you data from query and export it to a CSV formated file[code=php:0]<?// Your defined Q string// Used to get Data $result = mysql_query("select * from tmptbl WHERE field1 = dvalue");// Used to get field names $result1 = mysql_query("select * from tmptbl");// Check to see if there were and natching records else exit with message if(!$result) {$exitcode++; echo "No Records Found To Export<br>"; } else { echo "Matching Records Being Exported<br>"; }// if matching records were found export to a file (File name auto created based on date) if($exitcode == 0) {// Define file name $filename = date("HismdY") . ".csv";// Open file to write if (!$handle = fopen($filename, "wb")) {// If error exit program echo "Cannot open file " . $filename; exit(); } $printcolnames = 1;// loop thru qeury string to export data to CSV file while($row = mysql_fetch_array($result1)) { $rowval = ""; // ============ print column names ==================== if($printcolnames == 1) { $data = ""; $rowname = "";// First Row is field names for($i=0;$i<mysql_num_fields($result1);$i++) { $rowname .= mysql_field_name($result1, $i) . ","; } $rowname = substr($rowname,0,-1) . "\r\n"; if (!fwrite($handle, $rowname)) { echo "Cannot write to file $filename"; exit; } $printcolnames = 0; } // ========= end print column names =================== // ========= print col values ======================== while($row = mysql_fetch_assoc($result)) {// Create a row for every matching record foreach($row as $r) { $r = str_replace(",", " ", $r); $data .= $r . ","; } $data = substr($data,0,-1)."\r\n"; } if (!fwrite($handle, $data)){// echo "Cannot write to file $filename"; exit; } // ========= end print col values ===================// Close File fclose($handle);?>[/code] Link to comment https://forums.phpfreaks.com/topic/34146-copying-from-database/#findComment-161346 Share on other sites More sharing options...
grlayouts Posted January 18, 2007 Author Share Posted January 18, 2007 where will this file save to? Link to comment https://forums.phpfreaks.com/topic/34146-copying-from-database/#findComment-164002 Share on other sites More sharing options...
jvrothjr Posted January 19, 2007 Share Posted January 19, 2007 The location this file is placed. Same directory....as the code Link to comment https://forums.phpfreaks.com/topic/34146-copying-from-database/#findComment-164403 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.