Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
$username="root"; $password=""; $database="exam_codes";
$con = mysql_connect("localhost",$username,$password) or die( "Unable to Connect database");
mysql_select_db($database,$con) or die( "Unable to select database");
// Table Name that you want
// to export in csv
$ShowTable = "blogs";
$FileName = "_export.csv";
$file = fopen($FileName,"w");
$sql = mysql_query("SELECT * FROM `$ShowTable` LIMIT 11");
$row = mysql_fetch_assoc($sql);
// Save headings alon
$HeadingsArray=array();
foreach($row as $name => $value){
$HeadingsArray[]=$name;
}
fputcsv($file,$HeadingsArray);
// Save all records without headings
while($row = mysql_fetch_assoc($sql)){
$valuesArray=array();
foreach($row as $name => $value){
$valuesArray[]=$value;
}
fputcsv($file,$valuesArray);
}
fclose($file);
header("Location: $FileName");
echo "Complete Record saves as CSV in file: <b style=\"color:red;\">$FileName</b>";
?>
@huykon

This comment has been minimized.

Copy link

@huykon huykon commented Jul 4, 2017

it's not working

@Nor1m

This comment has been minimized.

Copy link

@Nor1m Nor1m commented Mar 31, 2018

workin, thanks.

@Aecttann

This comment has been minimized.

Copy link

@Aecttann Aecttann commented Apr 3, 2018

it's working

@AlbertTCB

This comment has been minimized.

Copy link

@AlbertTCB AlbertTCB commented Apr 9, 2018

Thanks! But unfortunately I get a HTTP ERROR 500?

@touss1

This comment has been minimized.

Copy link

@touss1 touss1 commented Jun 17, 2018

Works Like a CHARM.. THANK YOU

@brunotdantas

This comment has been minimized.

Copy link

@brunotdantas brunotdantas commented Nov 21, 2018

Perfect, it worked just fine, the only problem i found is when its exporting to the txt it uses comma as separator, the only change i made was to use the semicolon (;) as separator so the excel identifies and separate the columns automatically.

So when populating the CSV you can do like this

fputcsv($file,$HeadingsArray,";");

Forked and updated the php code with MSSQL connection if anyone need it

https://gist.github.com/brunotdantas/727faa4d5a47dd814c47a246af6ccf0e

@sirsotisix

This comment has been minimized.

Copy link

@sirsotisix sirsotisix commented Aug 7, 2020

Hello! I hope it's not too late to ask for help.

I am able to display the headers, but I am unable to display the remaining rows

I do the same sql query to show the results in a table within the web, but when I use it to generate the csv it doesn't show anything

Can you give me a hand please?

` if(isset($_POST["exportarCSV"])) {
$ShowTable = "****";

        $FileName = "_export.csv";
        $file = fopen($FileName,"w");
        
        $sql = mysqli_query($con,"SELECT * FROM **** WHERE RECINTO = '$RECINTO' AND FECHA BETWEEN '$DESDE' AND '$HASTA' ORDER BY FECHA,INICIO, RECINTO ASC");
        $row = mysqli_fetch_assoc($sql);
        // Save headings alon
        	$HeadingsArray=array();
        	foreach($row as $name => $value){
        		$HeadingsArray[]=$name;
        	}
        	fputcsv($file,$HeadingsArray,";");
        	
        // Save all records without headings
        
        	while($row = mysqli_fetch_assoc($sql)){
        	$valuesArray=array();
        		foreach($row as $name2 => $value){
        		$valuesArray[]=$name2;
        		}
        	fputcsv($file,$valuesArray); 
        	}
        	fclose($file);
        
        header("Location: $FileName");
        
        echo "Complete Record saves as CSV in file: <b style=\"color:red;\">$FileName</b>";

}`

@blinoale

This comment has been minimized.

Copy link

@blinoale blinoale commented Sep 15, 2021

Hi,
This script works well, but in csv file there is no first line of the table. List is starting from line 2. What may be the problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment