无法获取数据

时间:2019-06-06 21:20:21

标签: php mysql database mysqli

我正在尝试获取数据库文件,该文件的格式为csv(excel文件),但它甚至没有将数据发送到浏览器。

我还没有尝试过任何东西,因为我恐怕会做得比现在更糟...

我的PHP代码(我已经连接到数据库,但是我不想公开显示我的信息):

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// Create connection
$db = new mysqli($dbhost, $dbusername, $dbpassword, $dbname);

$allData = "";
$query = 'SELECT * FROM members';
  $retval = mysqli_query( $db,$query );
   if(! $retval ) {
      die('Could not get data.');
   }
   while($row = mysqli_fetch_array($retval)) {
  $allData .= $row['Id'] . ',' . $row['Email'] . ',' . $row['Condition'] .     ',' . $row['isEmailConfirmed'] . ',' . $row['Name'] . ',' .         $row['LastName'] . ',' . $row['Token'] . ',' .$row['Type'] . ',' .         $row['extra_privileges'] . ',' . $row['access-to-ftp'] . ',' .$row['access-    to-panel'] . ',' . $row['Birthdate'] . ',' . $row['Password'] . ',' .     $row['Description'] . "\n";
  //$retval = "data:text/csv;charset=utf-    8,ID,EMAIL,CONDITION,ISEMAILCONFIRMED,NAME,LASTNAME,TOKEN,TYPE,EXTRA_PRIVIL    EGES,ACCESS-TO-FTP,ACCESS-TO-PANEL,BIRTHDATE,PASSWORD,DESCRIPTION\n";
  $retval .= $allData;

  echo $retval;
  }
?>

这只是告诉我无法获取数据,这是我的解决之道。

1 个答案:

答案 0 :(得分:1)

您的问题是您正在重新使用相同的变量,并且丢失了数据库结果。

$sql = 'SELECT * FROM members';
$retval = mysqli_query($db, $sql);
// ^^^^ This PHP variable holds your DB results
if (!$retval) {
    die('Could not get data.');
}
//  Each iteration of the loop you reach for a single row from the result $retval
while ($row = mysqli_fetch_array($retval)) {
    $allData .= $row['Id'] . ',' . $row['Email'] . ',' . $row['Condition']     . ',' . $row['isEmailConfirmed'] . ',' . $row['Name'] . ',' .     $row['LastName'] . ',' . $row['Token'] . ',' .$row['Type'] . ',' .     $row['extra_pivileges'] . ',' . $row['access-to-ftp'] . ',' .$row['access-to    panel'] . ',' . $row['Birthdate'] . ',' . $row['Password'] . ',' .     $row['Description'] . "\n";
    $retval = "data:text/csv;charset=utf-8,ID,EMAIL,CONDITION,ISEMAILCONFIRMED,NAME,LASTNAME,TOKEN,TYPE,EXTRA_PRIVILEG    ES,ACCESS-TO-FTP,ACCESS-TO-PANEL,BIRTHDATE,PASSWORD,DESCRIPTION\n";
    $retval .= $allData;
    // ^^^ but here you are using the same variable, thus losing its previous contents. 

    echo $retval;
}

更改您的PHP结果的名称,请勿覆盖它。例如:

$sql = 'SELECT * FROM members';
$result_of_DB_select = mysqli_query($db, $sql);
// ^^^^ This PHP variable holds your DB results
if (!$result_of_DB_select) {
    die('Could not get data.');
}

// CSV header before the loop:
$CSV_response = "data:text/csv;charset=utf-8,ID,EMAIL,CONDITION,ISEMAILCONFIRMED,NAME,LASTNAME,TOKEN,TYPE,EXTRA_PRIVILEG    ES,ACCESS-TO-FTP,ACCESS-TO-PANEL,BIRTHDATE,PASSWORD,DESCRIPTION\n";

//  Each iteration of the loop you reach for a single row from the result variable called $result_of_DB_select 
while ($row = mysqli_fetch_array($result_of_DB_select)) {
    $CSV_response .= $row['Id'] . ',' . $row['Email'] . ',' . $row['Condition']     . ',' . $row['isEmailConfirmed'] . ',' . $row['Name'] . ',' .     $row['LastName'] . ',' . $row['Token'] . ',' .$row['Type'] . ',' .     $row['extra_pivileges'] . ',' . $row['access-to-ftp'] . ',' .$row['access-to    panel'] . ',' . $row['Birthdate'] . ',' . $row['Password'] . ',' .     $row['Description'] . "\n";
    // ^^^ different variable holds your CSV response now and you will not lose contents of $result_of_DB_select. 
}
echo $CSV_response;