儘管有描述如何使用PHP 和MySQL 在HTML 中建立表格的帖子,但之後頻繁更改MySQLSQL 列標題表可能會很麻煩。本文探討了一種自動更新 PHP 程式碼的方法,讓您可以指定表格名稱並列印該表,而無需手動插入
$table = "user"; $database = "database"; $conn = mysqli_connect("localhost", "username", "password", "database", "3306"); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT * FROM $table"; $result = $conn->query($sql); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { echo "<tr><td>" . $row["id"] . "</td><td>" . $row["first_name"] . "</td><td>" . $row["last_name"] . "</td><td>" . $row["birthday"] . "</td></tr>"; } echo "</table>"; } else { echo "0 result"; } $conn->close();
為了簡化流程,可以建立一個函數來動態執行這些操作。它檢查表是否存在,獲取數據,並產生帶有標題的 HTML 表。
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $conn = mysqli_connect("localhost", "username", "password", "database", "3306"); $mysqli->set_charset('utf8mb4'); // always set the charset function outputMySQLToHTMLTable(mysqli $mysqli, string $table) { // Verify table existence $tableNames = array_column($mysqli->query('SHOW TABLES')->fetch_all(), 0); if (!in_array($table, $tableNames, true)) { throw new UnexpectedValueException('Unknown table name provided!'); } $res = $mysqli->query('SELECT * FROM ' . $table); $data = $res->fetch_all(MYSQLI_ASSOC); echo '<table">'; // Table header echo '<thead>'; echo '<tr>'; foreach ($res->fetch_fields() as $column) { echo '<th' . htmlspecialchars($column->name) . '</th>'; } echo '</tr>'; echo '</thead>'; // Table body if ($data) { foreach ($data as $row) { echo '<tr>'; foreach ($row as $cell) { echo '<td' . htmlspecialchars($cell) . '</td>'; } echo '</tr>'; } } else { echo '<tr><td colspan="' . $res->field_count . '">No records in the table!</td></tr>'; } echo '</table>'; } outputMySQLToHTMLTable($mysqli, 'user');
$pdo = new PDO("mysql:host=localhost;dbname=test;charset=utf8mb4", 'username', 'password', [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false ]); function outputMySQLToHTMLTable(pdo $pdo, string $table) { // Verify table existence $tableNames = $pdo->query('SHOW TABLES')->fetchAll(PDO::FETCH_COLUMN); if (!in_array($table, $tableNames, true)) { throw new UnexpectedValueException('Unknown table name provided!'); } $stmt = $pdo->query('SELECT * FROM ' . $table); $data = $stmt->fetchAll(PDO::FETCH_ASSOC); $columnCount = $stmt->columnCount(); echo '<table">'; // Table header echo '<thead>'; echo '<tr>'; for ($i = 0; $i < $columnCount; $i++) { echo '<th' . htmlspecialchars($stmt->getColumnMeta($i)['name']) . '</th>'; } echo '</tr>'; echo '</thead>'; // Table body if ($data) { foreach ($data as $row) { echo '<tr>'; foreach ($row as $cell) { echo '<td' . htmlspecialchars($cell) . '</td>'; } echo '</tr>'; } } else { echo '<tr><td colspan="' . $columnCount . '">No records in the table!</td></tr>'; } echo '</table>'; } outputMySQLToHTMLTable($pdo, 'user');
此最佳化解決方案驗證使用更有效率的查詢的表存在性:
$tableNames = $pdo->prepare('SELECT COUNT(1) FROM information_schema.TABLES WHERE TABLE_SCHEMA = SCHEMA() AND TABLE_NAME=?'); $tableNames->execute([$table]); if (!$tableNames->fetchColumn()) { throw new UnexpectedValueException('Unknown table name provided!'); }
以上是如何使用 PHP 從 MySQL 資料庫動態產生 HTML 表?的詳細內容。更多資訊請關注PHP中文網其他相關文章!