使用PHP 在單獨的Excel 單元格中檢索MySQL 資料
您的原始PHP 腳本將所有文字值組合到一個Excel 單元在格中,這與您的衝突所需的單獨行輸出。為了修正此問題,我們將修改程式碼以將資料正確格式化為不同的儲存格。
以下更新的程式碼包括以下變更:
- 單獨的行: mysql_fetch_row() 用於逐行擷取資料。
- 格式化Headers:標題行填入 MySQL 欄位名稱。
- 格式化資料行:解析資料行,以製表符分隔,並引用 Excel 相容性。
- Excel 輸出:格式化資料回顯為 Excel 相容的 (.xls)檔案。
<?php // MySQL connection details $DB_Server = "localhost"; $DB_Username = "username"; $DB_Password = "password"; $DB_DBName = "databasename"; $DB_TBLName = "tablename"; // Define Excel file name $filename = "exportfile.xls"; // Connect to MySQL $Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Couldn't connect to MySQL: " . mysql_error()); // Select database $Db = @mysql_select_db($DB_DBName, $Connect) or die("Couldn't select database: " . mysql_error()); // Execute query $sql = "SELECT * FROM $DB_TBLName"; $result = @mysql_query($sql, $Connect) or die("Couldn't execute query: " . mysql_error()); // Set CSV separator (tabs) $sep = "\t"; // Print column names as headers for ($i = 0; $i < mysql_num_fields($result); $i++) { echo mysql_field_name($result, $i) . $sep; } echo "\n"; // Print data rows while ($row = mysql_fetch_row($result)) { $line = ''; for ($j = 0; $j < mysql_num_fields($result); $j++) { if (empty($row[$j])) { $line .= "NULL" . $sep; } else { $line .= '"' . str_replace('"', '""', $row[$j]) . '"' . $sep; } } $line = trim($line); echo $line . "\n"; } // Close MySQL connection mysql_close($Connect); // Set file headers for Excel header("Content-Type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename=$filename"); header("Pragma: no-cache"); header("Expires: 0"); // Echo data to Excel file echo $line . "\n";
此更新的程式碼將產生一個 Excel 文件,其中資料分為各個儲存格,滿足您的要求。
以上是如何使用 PHP 將 MySQL 資料匯出到單獨的 Excel 儲存格?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

mySqlStringTypesimpactStorageAndPerformanCeaseAsfollows:1)長度,始終使用theSamestoragespace,whatcanbefasterbutlessspace-felfficity.2)varCharisvariable varcharisvariable length,morespace-morespace-morespace-effficitybuteftife buteftife butfority butfority textifforlyslower.3)

mysqlStringTypesIncludeVarChar,文本,char,Enum和set.1)varCharisVersAtileForvariable-lengthStringStringSuptoPuptOuptoPepePecifiedLimit.2)textisidealforlargetStortStorStoverStoverStorageWithoutAutAdefinedLength.3)charlisfixed-lenftenge,for forConsistentDatalikeCodes.4)

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

toadduserInmysqleffect和securly,跟隨台詞:1)USEtheCreateUserStattoDaneWuser,指定thehostandastrongpassword.2)GrantNecterAryAryaryPrivilegesSustherthing privilegesgeStatement,usifementStatement,adheringtotheprinciplelastprefilegege.3)

toaddanewuserwithcomplexpermissionsinmysql,loldtheSesteps:1)創建eTheEserWithCreateuser'newuser'newuser'@''localhost'Indedify'pa ssword';。 2)GrantreadAccesstoalltablesin'mydatabase'withGrantSelectOnMyDatabase.to'newuser'@'localhost';。 3)GrantWriteAccessto'

MySQL中的字符串數據類型包括CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT,排序規則(Collations)決定了字符串的比較和排序方式。 1.CHAR適合固定長度字符串,VARCHAR適合可變長度字符串。 2.BINARY和VARBINARY用於二進制數據,BLOB和TEXT用於大對像數據。 3.排序規則如utf8mb4_unicode_ci忽略大小寫,適合用戶名;utf8mb4_bin區分大小寫,適合需要精確比較的字段。

最佳的MySQLVARCHAR列長度選擇應基於數據分析、考慮未來增長、評估性能影響及字符集需求。 1)分析數據以確定典型長度;2)預留未來擴展空間;3)注意大長度對性能的影響;4)考慮字符集對存儲的影響。通過這些步驟,可以優化數據庫的效率和擴展性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SublimeText3漢化版
中文版,非常好用

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中