2015-07-11 40 views
-1

當我運行此查詢"SELECT name , domain FROM my_data ORDER by id"到phpmyadmin的結果是這樣的:出口的MySQL結果陣列脫穎而出

 name domain 
    Bahamdan Group Holding Co. salam.com 
    ARINA RAMLEE salam.net 

這裏是我的代碼:

$con1 = mysqli_connect("localhost", "root", "", "test_pr"); 
     $sql2 = "SELECT name , domain FROM my_data ORDER by id"; 
     $result2 = mysqli_query($con1, $sql2); 
     $rows = array(); 
     while ($row = mysqli_fetch_array($result2, MYSQLI_ASSOC)) { 
      $rows[] = $row . PHP_EOL; 
     } 
     $nn = implode("", $rows); 
     var_dump($rows); 
     echo $nn . PHP_EOL; 
     $file = fopen("export.csv", "w"); 
     file_put_contents("export.csv", $nn); 
     fclose($file); 

但它不工作... 我想把它放在一個完全像在phpmyadmin中顯示的文件中。 我的意思是我想在一個excel文件中有兩列,其中一列是名稱,另一列是域,並在列下有行數據。 我試過這個,這是工作,但我不知道如何創建我想用這個文件。

$con1 = mysqli_connect("localhost", "root", "", "test_pr"); 
     $sql2 = "SELECT name , domain FROM my_data ORDER by id"; 
     $result2 = mysqli_query($con1, $sql2); 
     $rows = array(); 
     while ($row = mysqli_fetch_array($result2, MYSQLI_ASSOC)) { 
      $domains[] = $row['domain'] . PHP_EOL; 
      $name[] = $row['name']. PHP_EOL; 
     } 

var_dump($domains); 
var_dump($name); 
     $nn = implode("", ??????); 
     $file = fopen("export.csv", "w"); 
     file_put_contents("export.csv", $nn); 
     fclose($file); 

這裏是vardump

array (size=2) 
    0 => string 'salam.com 
' (length=11) 
    1 => string 'salam.net 
array (size=2) 
    0 => string 'Bahamdan Group Holding Co. 
' (length=28) 
    1 => string 'ARINA RAMLEE 
+1

什麼不工作?你可以清理你的文章,擺脫所有無用的空間:如果你需要幫助,它必須是可讀的。 –

+1

PHP有一個很好的叫做fputcsv()的小功能,並且[documentation](http://php.net/manual/en/function.fputcsv.php)演示瞭如何使用它來編寫一個csv文件 –

+0

我想要的要做的是不工作...想要創建一個有兩列,幷包含域和名稱數據的excel文件(就像我在phpmyadmin中看到的那樣) –

回答

0

下面是示例代碼通過使用用於獲取數據MySQL的查詢來生成Excel表。

//create query to select as data from your table 
$select = "SELECT * FROM table_name"; 

//run mysql query and then count number of fields 
$export = mysql_query ($select) 
     or die ("Sql error : " . mysql_error()); 
$fields = mysql_num_fields ($export); 

//create csv header row, to contain table headers 
//with database field names 
for ($i = 0; $i < $fields; $i++) { 
    $header .= mysql_field_name($export , $i) . ","; 
} 

//this is where most of the work is done. 
//Loop through the query results, and create 
//a row for each 
while($row = mysql_fetch_row($export)) { 
    $line = ''; 
    //for each field in the row 
    foreach($row as $value) { 
     //if null, create blank field 
     if ((!isset($value)) || ($value == "")){ 
      $value = ","; 
     } 
     //else, assign field value to our data 
     else { 
      $value = str_replace('"' , '""' , $value); 
      $value = '"' . $value . '"' . ","; 
     } 
     //add this field value to our row 
     $line .= $value; 
    } 
    //trim whitespace from each row 
    $data .= trim($line) . "\n"; 
} 
//remove all carriage returns from the data 
$data = str_replace("\r" , "" , $data); 


//create a file and send to browser for user to download 
header("Content-type: application/vnd.ms-excel"); 
header("Content-disposition: csv" . date("Y-m-d") . ".csv"); 
header("Content-disposition: filename=".$file_name.".csv"); 
print "$header\n$data"; 
exit; 
+0

何時開發人員將停止使用糟糕的自制解決方案編寫csv文件,並開始使用PHP內置的[ fputcsv()](http://php.net/manual/en/function.fputcsv.php)函數? –