VanillaRose Posted June 1, 2015 Share Posted June 1, 2015 Hi, I have a non-profit website that I've developed with PHP. I'm trying to make the logged in users can output letters in Word with selected data from a query in MySQL. I've tried to use PHPWord, the letters outputted with no data in the letters. Also I tried used PHP COM, but Hostforlife won't let me restart the server because it is shared. Lastly I wrote a SELECT query with INTO OUTFILE '$file' to create a CSV which the users could download and use to merge into Word, but because the database user does not have FILE access, it produces this error: Error: Access denied for user 'blabla'@'localhost' (using password: YES) I think Hostforlife will not allow FILE privileges on a shared server. So, how can I get around this. Somehow I want to be able to at least write the query and download the data. Javascript? jQuery? or Something else? Any of you can help me? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/296581-how-i-can-i-let-user-to-download-a-data-from-query-in-mysql/ Share on other sites More sharing options...
Barand Posted June 1, 2015 Share Posted June 1, 2015 This is the download function that I use. function sql2csv($mysqli, $sql, $filename='', $headings=1) /** * Parameters * $mysqli - connection * $sql - the sql query to be executed * $filename - name of download file (default "download_yymmddhhii.csv") * $headings - 1 if fieldname headings required (default), 0 if not required */ { if (!$filename) $f = 'download_' . date('ymdhi') . '.csv'; else $f = $filename; $fp = fopen('php://output', 'w'); // so you can fputcsv to STDOUT if ($fp) { $res = $mysqli->query($sql); if ($res) { header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="'.$f.'"'); header('Pragma: no-cache'); header('Expires: 0'); $row = $res->fetch_assoc(); if ($headings) { fputcsv($fp, array_keys($row)); } do { fputcsv($fp, $row); } while ($row = $res->fetch_assoc()); } else echo "Error in query"; fclose($fp); } } Use it in a php file that you link to with a download link. Quote Link to comment https://forums.phpfreaks.com/topic/296581-how-i-can-i-let-user-to-download-a-data-from-query-in-mysql/#findComment-1512967 Share on other sites More sharing options...
moosey_man1988 Posted August 11, 2015 Share Posted August 11, 2015 apologies, this was my first function, I have the function working now, but still the excel file contains the echo's from the webpage + the data from the the query.. like so <head> <title>Newfies Page</title> </head> <body> <h2>Lets take a look at a table </h2> exporting!</br>created newfies table</br>prepared 1000 numbers7415354264 7428357984 7428748979 7500332817 7504051767 7504299246 7504822797 7505149835 7506677762 7506878021 7510330942 7510935466 7510941052 7511362543 7511732442 7511889806 7512215911 7512575544 7513832701 Here is my code: require('test.php'); sql2csv($db_mysqli, $sql, $filename='myNewfile.csv', $headings=0); } ?> <?php $sql = 'SELECT * FROM newfies'; function sql2csv($db_mysqli, $sql, $filename='', $headings=0) { if (!$filename) $f = 'download_' . date('ymdhi') . '.csv'; else $f = $filename; $fp = fopen('php://output', 'w'); // so you can fputcsv to STDOUT if ($fp) { $res = $db_mysqli->query($sql); if ($res) { header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="'.$f.'"'); header('Pragma: no-cache'); header('Expires: 0'); $row = $res->fetch_assoc(); if ($headings) { fputcsv($fp, array_keys($row),'|'); } do { fputcsv($fp, $row); } while ($row = $res->fetch_assoc()); } else echo "Error in query"; fclose($fp); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/296581-how-i-can-i-let-user-to-download-a-data-from-query-in-mysql/#findComment-1518482 Share on other sites More sharing options...
Barand Posted August 11, 2015 Share Posted August 11, 2015 File 1 <a href="file2.php">Download newfies</a> File2.php (the download code goes in a file of its own) <?php $sql = 'SELECT * FROM newfies'; sql2csv($db_mysqli, $sql, $filename='myNewfile.csv', $headings=0); function sql2csv($db_mysqli, $sql, $filename='', $headings=0) { if (!$filename) $f = 'download_' . date('ymdhi') . '.csv'; else $f = $filename; $fp = fopen('php://output', 'w'); // so you can fputcsv to STDOUT if ($fp) { $res = $db_mysqli->query($sql); if ($res) { header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="'.$f.'"'); header('Pragma: no-cache'); header('Expires: 0'); $row = $res->fetch_assoc(); if ($headings) { fputcsv($fp, array_keys($row),'|'); } do { fputcsv($fp, $row); } while ($row = $res->fetch_assoc()); } else echo "Error in query"; fclose($fp); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/296581-how-i-can-i-let-user-to-download-a-data-from-query-in-mysql/#findComment-1518529 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.