簡體   English   中英

嘗試在HTML表中顯示MySQL查詢的結果

[英]Trying to show results from MySQL query in a HTML table

我試圖從數據庫表中獲取一些簡單的信息,並將其發布在HTML頁面上。 看起來非常簡單明了,沒有錯誤,這意味着我連接到數據庫和表。 我已經有所有數據了(手動輸入)。

我假設這是我調用列名進行查詢的方式。

<?php
    require_once("settings.php");
        //Open Connection
    $conn = @mysqli_connect("$host","$user","$pswd")
        or die ('Failed To Connect to Server');
    @mysqli_select_db("$conn", "$dbnm")
        or die ('Database Not Available');
        //Set up SQL string and excecute
    $car_id = mysqli_escape_string($_GET['car_id']);
    $make = mysqli_escape_string($_GET['make']);
    $model = mysqli_escape_string($_GET['model']);
    $price = mysqli_escape_string($_GET['price']);

    $query = "SELECT car_id, make, model, price FROM cars";

    $results = mysqli_query($conn, $query);

        echo "<table width ='100%' border='1'>
        <tr>
        <th>car_id</th>
        <th>make</th>
        <th>model</th>
        <th>price</th>
        </tr>";

        //  $row = mysqli_fetch_row($query);
                while ($row = mysqli_fetch_array($results)) {
                echo "<tr>";
                echo "<td>" . $row['car_id'] . "</td>";
                echo "<td>" . $row['make'] . "</td>";
                echo "<td>" . $row['model'] . "</td>";
                echo "<td>" . $row['price'] . "</td>";
                echo "</tr>";
        //  $row = mysqli_fetch_row($query);
                        }
        echo "</table>";
    //Close Connection
mysqli_free_result($results);
mysqli_close($conn);
?>  

settings.php保存所有連接信息,並全部簽出。 我什至需要($ _GET ['car_id'])等嗎? 我可以用字段名稱來稱呼他們嗎?

答案將是如此明顯……

我沒有錯誤,這意味着我已連接到數據庫和表

這並不意味着。

mysqli_select_db("$conn", "$dbnm")  // That `$conn` should not be inside those quotes.

應該

mysqli_select_db($conn, $dbnm);  // that $conn has to be a MySQLi link identifier, not an interpolated one.

還要刪除所有@,並對這些函數返回的內容進行一些錯誤檢查。

請如下更改以下行。

$conn = @mysqli_connect("$host","$user","$pswd")

$conn = mysqli_connect($host,$user,$pswd)

 @mysqli_select_db("$conn", "$dbnm")

 mysqli_select_db($conn, $dbnm)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM