简体   繁体   English

尝试在HTML表中显示MySQL查询的结果

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

I am trying to get some simple information from a DB table and post it on a HTML page. 我试图从数据库表中获取一些简单的信息,并将其发布在HTML页面上。 It's seems pretty simple and straight forward, I get no errors which means I connect to the DB and the table. 看起来非常简单明了,没有错误,这意味着我连接到数据库和表。 I have all the data already there (entered manually). 我已经有所有数据了(手动输入)。

I am assuming that it is in the way I call the column names to make the query. 我假设这是我调用列名进行查询的方式。

<?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 holds all the connection info and it all checks out. settings.php保存所有连接信息,并全部签出。 Do I even need the ($_GET['car_id']), etc? 我什至需要($ _GET ['car_id'])等吗? Can I just call them by the field name? 我可以用字段名称来称呼他们吗?

The answer is gonna be so obvious... 答案将是如此明显……

I get no errors which means I connect to the DB and the table 我没有错误,这意味着我已连接到数据库和表

It does not mean that. 这并不意味着。

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

Should be 应该

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

Also remove all the @ and do some error checking as to what those functions return. 还要删除所有@,并对这些函数返回的内容进行一些错误检查。

Please change the following line as below. 请如下更改以下行。

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

to

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

And

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

to

 mysqli_select_db($conn, $dbnm)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM