MyBB Community Forums

Full Version: Data Extracts from MyBB database
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all,

I am going to create some preset reports for my admins to run on the users of our site.

Basically I believe I am able to create a link that will run an sql-query that I define on the database....however I don't necessarily want to show the results on a formatted page, but rather pipe it to csv-file....now how would I do it?

So there would be for example a link that runs the following:

SELECT mybb_users.uid, mybb_userfields_firstname, mybb_userfields_lastname, mybb_userfields_street1, mybb_userfields_street2, mybb_userfields_street3, mybb_userfields_city, mybb_userfields_postcode, mybb_country_countryname
FROM mybb_users, mybb_userfields, mybb_country
WHERE mybb_users.hardcopyapproval='YES' AND (mybb_users.uid=mybb_userfields.ufid) AND (mybb_users.countryid=mybb_countryid) AND mybb_users.addressquality='MAILABLE';

(quite obviously a query to pull mailing-labels for a hardcopy-mailing)

this would then need to pop-up a 'file save -dialogue' with a CSV-file with proper encoding to protect the international characters...

can anyone help me?

cheers,
Kimmo
So you want to extract information from the DB and put it into a csv file?

header('content-type: text/plain');
$query = $db->query([i]your query[/i]);
if($query){
while($row = $db->fetch_array($query)){
echo implode(",", $row) . "\n";
}
}

Of course if the address has a comma in it, it will mess up your file. The best thing to do would be to use a tab character. If you want to copy/paste the info into excel you can and it will put it in columns correctly.
The above is the simplest way to do that.

EDIT:
If you want the file to be downloaded, add the code
header('Content-Disposition: attachment; filename=[i]filename[/i]');
Below the other header statement.
good stuff...I will be trying this on for a size in a few days time Smile

cheers,
Kimmo