将 MySQL 中的数据显示为 HTML

问题描述 投票:0回答:0

`

Table with Database




<?php
// Replace these credentials with your actual MySQL database details
$host = 'localhost';
$username = 'root';
$password = '';
$dbname = 'khb_db';

// Create a new MySQLi connection
$conn = new mysqli($host, $username, $password, $dbname);

// Check connection
if ($conn -> connect_error) {
    die("Connection failed: ".$conn->connect_error);
}

// Query to fetch data from the database
$sql = "SELECT `Sl.No`, `Site No`, `Site Type`, `Owner's Name`, `Contact Number`, `Address` FROM purchasers_contact_details";

// Execute the query
$result = $conn->query($sql);

// Check if there are any results
if ($result->num_rows > 0) {
    // Start building the HTML table
    echo '<table>';
    echo '<tr><th>Sl.No</th><th>Site No</th><th>Site Type</th><th>Owner\'s Name</th><th>Contact Number</th><th>Address</th></tr>';

    // Loop through the result set and display the data in the table
    while ($row = $result->fetch_assoc()) {
        echo '<tr>';
        echo '<td>' . $row["Sl.No"] . '</td>';
        echo '<td>' . $row["Site No"] . '</td>';
        echo '<td>' . $row["Site Type"] . '</td>';
        echo '<td>' . $row["Owner's Name"] . '</td>';
        echo '<td>' . $row["Contact Number"] . '</td>';
        echo '<td>' . $row["Address"] . '</td>';
        echo '</tr>';
    }

    // End the table
    echo '</table>';
} else {
    // If there are no results, display the "0 results" message
    echo '0 results';
}

// Close the MySQL connection
$conn->close();

This is the current output, help me fix this

我已经尝试了所有可能的解决方案,也尝试了 ChatGPT,但似乎没有任何效果? 只是填补空白,以便我可以发布我的问题。预先感谢您的回复 只是填补空白,以便我可以发布我的问题。预先感谢您的回复 只是填补空白,以便我可以发布我的问题。预先感谢您的回复 只是填补空白,以便我可以发布我的问题。先谢谢您的回复`

只是填补空白,以便我可以发布我的问题。先谢谢您的回复

php html mysqli
© www.soinside.com 2019 - 2024. All rights reserved.