Yes another csv question, I have found many topics but not what I need, I only want to export 4 columns from my database, and also specify column headings for the column names... Is that possible?
Yes another csv question, I have found many topics but not what I need, I only want to export 4 columns from my database, and also specify column headings for the column names... Is that possible?
*wince* CSV? Why?
CSV is all very well and good until you have one of your seperator characters in your data. Then you have to implement all sorts of things to get it working. You'd be better off using, say, XML. PHP has a standard config file format, too, which it can read in natively; see parse_ini_file().
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
What you should do is use mysql_fetch_array to select the rows you want, and then format it so that it looks like a CSV format. From there, cut and paste it into Notepad and save it as .csv format.
Example:
Would look like this:Code:$result = mysql_fetch_array( $result2 ); echo "$result2[row1] , $result2[row2]"
info1 , info2
Good luck
Actually you need braces.If you're doing that, though, you might as well:Code:echo "{$result2['row1']} , {$result2['row2']}";Code:function mysql_fetch_all($rs) { $arr = array(); while($row = mysql_fetch_array($rs)) array_push($arr, $row); return $arr; } function mysql_to_csv($rs) { return implode("\n", array_map(create_function('$a', 'return implode(\',\', $a);'), mysql_fetch_all($rs))); }
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Thanks guys... Well I got it to run, but it doesn't save anything... This is what I have... Should I be using fputcsv instead? Oh and the reason it's a csv is so it can be imported into a simple gateway app on the web and they only take .csv unfortunately...
PHP Code:$query = "";
$result = mysql_query($query);
$num = mysql_num_rows($result);
function mysql_fetch_all($result) {
$arr = array();
while($row = mysql_fetch_array($result))
array_push($arr, $row);
return $arr;
}
function mysql_to_csv($result) {
return implode("\n", array_map(create_function('$a', 'return implode(\',\', $a);'), mysql_fetch_all($result)));
}
include 'library/closedb.php';
?>
Why do you expect it to? Surprisingly enough, the mysql_to_csv() function converts a MySQL result resource to CSV. You haven't actually done anything with that data -- in fact, you haven't even called the function.it doesn't save anything...
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
But when the page runs isn't it called? I don't understand...
No... since I don't know what you want to do with the data, I provided two functions ("a portion of code within a larger program, which performs a specific task and is relatively independent of the remaining code"), the former of which is used by the latter to perform the conversion you required.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Um sorry that totally confuses me... What I want to do is pull out from my table into a csv file 5 columns, FirstName, LastName, Email, Mobile... I then want to add to the top of the .csv seperate headings... So basically I want to achieve this...
Fname, Sname, Email, Mobile
Tom, Smith, tom@smith.com, 0410000000
James, Smith, james@smith.com, 0410000000
I have tried every which way and can't get a result... I'm not sure where to go from this point... How do I write to the actual .csv file? I mean what do I need to add to the above code to get a file?
Hiya guys ok worked it all out... This appears to be working correctly, does it look ok?
PHP Code:$result = mysql_query("");
while($row = mysql_fetch_array($result)) {
$textString = $row['MobileNumber'];
$trimmedMobile = preg_replace('/[^\d+]/', '', $MobileNumber);
$csv_output .= "$row[FirstName],$row[LastName],$trimmedMobile,$row[Email]\n";
}
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=" .$date.".xls");
print $csv_output;
exit;
?>
Last edited by tomyknoker; 08-21-2007 at 03:15 PM.
Bookmarks