简体   繁体   中英

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. 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. Do I even need the ($_GET['car_id']), etc? 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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