2014-02-23 63 views
0

我想從mysql表中導出一些數據到文本文件中。從mysql導出數據到文本文件?

但是,我只收到幾個,寫入我的文本文件中,但沒有members表中的實際數據!

我使用的代碼波紋管:

<?php 
    $fh = fopen('email-data.txt', 'w'); 
    include "../config/connect.php"; 

    /* insert field values into data.txt */ 

    $sql = "SELECT * FROM members"; 
    $query = mysqli_query($db_conx, $sql); 
    while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){   
     $num = mysqli_num_fields($query) ;  
     $last = $num - 1; 
     for($i = 0; $i < $num; $i++) {    
      fwrite($fh, $row[$i]);      
      if ($i != $last) { 
       fwrite($fh, ","); 
      } 
     }                 
     fwrite($fh, "\n"); 
    } 
    fclose($fh); 
?> 

可能有人請幫助我嗎?

+0

你爲什麼不存儲要在寫入文本一個變量,並在最後寫作?只有1'fwrite',在以後更容易調試和修改您正在使用'MYSQLI_ASSOC',它應該是'MYSQLI_NUM' – kero

+2

。那就是訣竅。 –

+0

@MuhammadHaseebKhan,謝謝你隊友imo – user3343724

回答

0

變化MYSQLI_ASSOCMYSQLI_NUM

你取行與Numbers但使用Assiciative陣列

0

這樣的事情?您將收到一個不帶數字索引的關聯數組

<?php 
$fh = fopen('email-data.txt', 'w'); 
include "../config/connect.php"; 

/* insert field values into data.txt */ 

$sql = "SELECT * FROM members"; 
$query = mysqli_query($db_conx, $sql); 
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){   
    //$num = mysqli_num_fields($query) ;  
    //$last = $num - 1; 
    //for($i = 0; $i < $num; $i++) {    
    $last = sizeof($row)-1; 
    $i = 0; 
    foreach($row as $field) { 
     fwrite($fh, $field);      
     if ($i != $last) { 
      fwrite($fh, ","); 
     } 
     $i++; 
    }                 
    fwrite($fh, "\n"); 
} 
fclose($fh); 
?> 
0

更改mysqli_fetch_array()函數的第二個參數。

While循環應該像while($row = mysqli_fetch_array($query, MYSQLI_NUM)){...}