Mysql2csvUsingPhp

<?php
header('Content-type: text/x-csv');
header('Content-Disposition: attachment; filename="tpg.csv"');
 
include_once("/register_global.inc.php");
 
#produit un fichier csv
include("connect.php");
$laTable="nomDeVotreTableMySQL";
$db=connect_db();
$db_name=db_name();
mysql_select_db($db_name,$db);
$result = mysql_query("select * from " .$laTable);
$dbResult = mysql_list_fields($database_name, $laTable, $db);
#nom des champs
      //loop over each field
$x="\"";
        for($i = 0; $i < mysql_num_fields($dbResult); $i++)
        {
        $txt=mysql_field_name($dbResult, $i);
        $txt=ereg_replace(";", "\;", $txt);
$x.=$txt ."\";\"";
        }
$x.="\"\n";
$i = 0;
while ($i < mysql_num_rows($result)) {
$x.="\"";
        //loop over each field
        for($j = 0; $j < mysql_num_fields($dbResult); $j++)
        {
        $lechamp=mysql_field_name($dbResult, $j);
$txt=mysql_result($result,$i,$lechamp);
        $txt=ereg_replace(";", "\;", $txt); //pour les séparateurs de champ
       $txt=preg_replace("/(\015\012)|(\015)|(\012)/"," ", $txt); //caractères spéciaux
        $txt=ereg_replace("\r\n", " ", $txt); //retours chariot
        $txt=ereg_replace("\n", " ", $txt); //retours chariot
$txt=stripslashes($txt); //enlève backslashes
$txt=str_replace('\\',"", $txt);//enlève backslashes
 
$x.=$txt ."\";\"";
        }
$i++;
$x.="\"\n";
}
echo "source$x/source";
?>
  • info/fichier_mysql2csvusingphp.txt
  • Dernière modification: 2018/07/18 09:45
  • par radeff