[英]Export MySql table to CSV?
因此,我在堆棧和Google以及我能想到的任何地方搜索了大多數答案。 確實有很多答案,但是沒有一個我需要的結構。
我的代碼看起來像這樣:
$sql = "SELECT * FROM users WHERE user_level = '$level' ORDER BY user_username DESC";
if ($stmt = $this->connect->prepare($sql)) {
$stmt->bind_result($id);
$stmt->execute();
while ($row = $stmt->fetch()) {
$stmt->bind_result($id);
$users[] = $id;
}
$stmt->close();
$length[] = sizeof($users);
} else {
$error = true;
$message['error'] = true;
$message['message'] = CANNOT_PREPARE_DATABASE_CONNECTION_MESSAGE;
return json_encode($message);
}
我在所有代碼中都使用mysqli
和stmt
,因此我希望一直保持這種狀態。
因此,我了解到我無法通過操作按鈕下載CSV文件。 但是問題是我的操作按鈕是表單的一部分,所以我想不是CSV上的$_GET
而是CSV而是$_POST
。
我的問題是,如何循環遍歷所有數據庫(這取決於列,級別),並把所有數據整理成CSV文件並下載呢? 但這必須是我的函數結構,我不想使用db_query("SELECT * FROM {loreal_salons}");
每話或其他類似的話。
使用以下內容:
// Setup the headers to force download of CSV
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");
// loop your records here and output as comma separated
echo $row['col1'].",".$row['col3'].",".$row['col3']."\n";
如果您想遍歷所有數據庫,我想您要做的關鍵是:
顯示表-http: //dev.mysql.com/doc/refman/4.1/en/show-tables.html ..循環顯示所有表
顯示列-http: //dev.mysql.com/doc/refman/5.0/en/show-columns.html ..循環進入特定表的所有列
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.