Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm new on StackOverflow, I think this would help. (I speak spanish, I hope you to understand my english :D )</p> <p>I've been looking for a simply way to use mysqli and download a csv file that could be read by excel without UTF-8 problems (using ñ,á,ü...). I didn't found it, so I created one by myself (learning from Google and StackOverflow answers), after some hours I got something that works finally.</p> <p>This is a Class that connects with the database and the functions will do whatever you want using mysqli and PHP. In this case, calling this class (require or include), just use the "downloadCsv()" function.</p> <p>As an example, this would be the "class.php" file:</p> <pre><code>&lt;?php class DB{ private $con; //this constructor connects with the database public function __construct(){ $this-&gt;con = new mysqli("Your_Host","Your_User","Your_Pass","Your_DatabaseName"); if($this-&gt;con-&gt;connect_errno &gt; 0){ die('There was a problem [' . $con-&gt;connect_error . ']'); } } //create the function that will download a csv file from a mysqli query public function downloadCsv(){ $count = 0; $header = ""; $data = ""; //query $result = $this-&gt;con-&gt;query("SELECT * FROM Your_TableName"); //count fields $count = $result-&gt;field_count; //columns names $names = $result-&gt;fetch_fields(); //put column names into header foreach($names as $value) { $header .= $value-&gt;name.";"; } } //put rows from your query while($row = $result-&gt;fetch_row()) { $line = ''; foreach($row as $value) { if(!isset($value) || $value == "") { $value = ";"; //in this case, ";" separates columns } else { $value = str_replace('"', '""', $value); $value = '"' . $value . '"' . ";"; //if you change the separator before, change this ";" too } $line .= $value; } //end foreach $data .= trim($line)."\n"; } //end while //avoiding problems with data that includes "\r" $data = str_replace("\r", "", $data); //if empty query if ($data == "") { $data = "\nno matching records found\n"; } $count = $result-&gt;field_count; //Download csv file header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=FILENAME.csv"); header("Pragma: no-cache"); header("Expires: 0"); echo $header."\n".$data."\n"; } ?&gt; </code></pre> <p>After creating the "class.php" file, in this example, use that function on "download.php" file:</p> <pre><code>&lt;?php //call the "class.php" file require_once 'class.php'; //instantiate DB class $export = new DB(); //call function $export-&gt;downloadCsv(); ?&gt; </code></pre> <p>After download, open the file with MS Excel.</p> <p>I hope this help you, I think I wrote it well, I didn't feel comfortable with the text and code field.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload