Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save brunotdantas/727faa4d5a47dd814c47a246af6ccf0e to your computer and use it in GitHub Desktop.
Save brunotdantas/727faa4d5a47dd814c47a246af6ccf0e to your computer and use it in GitHub Desktop.
<?php
/***********************************************************
MYSQL VERSION
************************************************************/
$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>";
?>
<?php
<?php
/***********************************************************
MSSQL VERSION
************************************************************/
// Include the database connections
include '../config/configdb.php';
// Get some parameter if you need
$periodo = $_GET['periodo'];
// Your query
$sql = "
select distinct c.periodo,v.Nome,v.CPF,c.valor_a_pagar from comissoes_calculadas c
inner join vendedores v on c.idVendedor = v.idVendedor
where c.periodo = '$periodo'
";
// handle the result of the query to a variable
$resultado = sqlsrv_query( $conn, $sql);
$FileName = "_export.csv";
$file = fopen($FileName,"w");
// Save headings alon
$HeadingsArray=array();
// handle the results of the query to a variable
$row = sqlsrv_fetch_array($resultado, SQLSRV_FETCH_ASSOC) ;
// Get the first row (usually this is the column name of your query)
foreach($row as $name => $value){
$HeadingsArray[]=$name;
}
// out put the results to a csv
fputcsv($file,$HeadingsArray,";");
// Save all records without headings
while( $row = sqlsrv_fetch_array($resultado, SQLSRV_FETCH_ASSOC) ){
// get the lines and do the same as above
$valuesArray=array();
foreach($row as $name => $value){
$valuesArray[]=$value;
}
fputcsv($file,$valuesArray,";");
}
//close the file
fclose($file);
// directs the browser to your file, this is when the file is downloaded to the customer machine
header("Location: $FileName");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment