我正在尝试将一些数据从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 :(得分: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);
?>
答案 1 :(得分:0)
更改mysqli_fetch_array()
函数的第二个参数。
while循环应该看起来像while($row = mysqli_fetch_array($query, MYSQLI_NUM)){...}
答案 2 :(得分:0)
将MYSQLI_ASSOC
更改为MYSQLI_NUM
您正在使用Numbers
获取行但使用Assiciative数组