Jump to content

Export csv from two tables results


biggieuk

Recommended Posts

Hi all,

 

Im having a bit of trouble finding a solution as to how i can export a list of results to a .csv file. These results are returned from the database using the following two sql statements:

 

// To return a list of users that have the $groupid value in one of thier selections, selections are stored like (12,16,22,55)

"SELECT users.username FROM users WHERE users.selections REGEXP '[[:<:]]".$groupid."[[:>:]]'"

//At the top of the excel file there should be the name of the session, time & venue using this sql

"SELECT sessions.group,title,room,time FROM sessions WHERE groupid = ".$groupid"

 

Any advice would be great. I have tried editing an existing csv file but can only get it to return the list of users.

 

<?php require_once('../Connections/hwbltaconf.php'); 
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=SessionUsers.csv");
header("Pragma: no-cache");
header("Expires: 0");


mysql_select_db($database_hwbltaconf, $hwbltaconf);
$query = "SELECT users.username FROM users WHERE users.selections REGEXP '[[:<:]]11[[:>:]]'";
$exp = mysql_query($query);
$count = mysql_num_fields($exp);
for ($i = 0; $i < $count; $i++) {
$header .= mysql_field_name($exp, $i).",";
}
while($row = mysql_fetch_row($exp)) {
$line = '';
foreach($row as $value) {
if ((!isset($value)) OR ($value == "")) {
$value = "\t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . ",";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
$data = str_replace("\r", "", $data);
if ($data == "") {
$data = "\n(0) Records Found!\n";
}
print "$header\n$data";
exit;

?> 

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/149550-export-csv-from-two-tables-results/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.