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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.