2016-08-15 1 views
0

wenn ich ergebnis nehmen, dass keine tabelle informationen, nur vorhanden tabelle feldname! ein anderes Problem, das ist, alle Informationen in einer Spalte. dies ist mein Code:export mysql daten zu csv in php, nicht drucken informationen in csv-datei

<?php 

if(isset($_POST['sub_csv'])){ 
    $conn=mysql_connect("localhost","root",""); 
    mysql_select_db("school",$conn); 

    $filename='uploads/'.strtotime("now").'.csv'; 

    $fp=fopen($filename,"w"); 

    $sql=mysql_query("SELECT * FROM student"); 
    $num_rows=mysql_num_rows($sql); 
    if($num_rows >=1) 
    { 
    $row=mysql_fetch_assoc($sql); 

    $seperator=""; 
    $comma=""; 

    foreach($row as $name => $value) 
    { 
     $seperator .= $comma . '' .str_replace('','""',$name); 
     $comma=","; 
    } 
    $seperator .="\n"; 
    fputs($fp,$seperator); 
     fclose($fp); 
    } 

    else 
    { 
     echo 'No record in database'; 
    } 
} 
?> 

Ergebnis: enter image description here

Antwort

0

Sie direkt fputcsv() Funktion verwenden können, um CSV-Datei zu erstellen. Lesen Sie den folgenden Beispielcode und ändern Sie Ihren PHP-Code. Sie erhalten das erwartete Ergebnis.

// output headers so that the file is downloaded rather than displayed 
header('Content-Type: text/csv; charset=utf-8'); 
header('Content-Disposition: attachment; filename=data.csv'); 

// create a file pointer connected to the output stream 
$output = fopen('php://output', 'w'); 

// output the column headings 
fputcsv($output, array('Column 1', 'Column 2', 'Column 3')); 

// fetch the data 
mysql_connect('localhost', 'username', 'password'); 
mysql_select_db('database'); 
$rows = mysql_query('SELECT field1,field2,field3 FROM table'); 

// loop over the rows, outputting them 
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row); 

Für mehr zu lesen: 1. http://code.stephenmorley.org/php/creating-downloadable-csv-files/ 2. http://php.net/manual/en/function.fputcsv.php

+0

thanx, das neue Problem, das dieses nicht wahr ist, zeigen persische Informationen in der csv Akte. Ich füge diesen Code vor Auswahl Abfrage: mysql_query ('set Namen "utf8"'); [Ergebnisbild] [1] [1]: http://i.stack.imgur.com/WJZHy.png –

0

jetzt nur die erste Zeile holen $row=mysql_fetch_assoc($sql);

Sie müssen alle Zeilen nacheinander holen eine Weile Schleife wie diese while($row=mysql_fetch_assoc($sql)){..}

Basierend auf dem Code, den Sie zur Verfügung gestellt:

<?php 

if(isset($_POST['sub_csv'])){ 
    $conn=mysql_connect("localhost","root",""); 
    mysql_select_db("school",$conn); 

    $filename='uploads/'.strtotime("now").'.csv'; 

    $fp=fopen($filename,"w"); 

    $sql=mysql_query("SELECT * FROM student"); 
    $num_rows=mysql_num_rows($sql); 
    if($num_rows >=1) 
    { 
    while($row=mysql_fetch_assoc($sql)){ 

    $seperator=""; 
    $comma=""; 

    foreach($row as $name => $value) 
    { 
     $seperator .= $comma . '' .str_replace('','""',$name); 
     $comma=","; 
    } 
    $seperator .="\n"; 
    fputs($fp,$seperator); 
    } 
     fclose($fp); 
    } 

    else 
    { 
     echo 'No record in database'; 
    } 
} 
?> 
0

thanx, das neue Problem das ist nicht wahr zeigen persische Informationen in CSV-Datei. Ich füge diesen Code vor Select-Abfrage: mysql_query ('Set-Namen "utf8"'); [Ergebnisbild] [1] [1]: i.stack.imgur.com/WJZHy.png