2

我正在尝试从数据库表中获取一些简单的信息并将其发布在 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']) 等吗?我可以通过字段名称来称呼他们吗?

答案会很明显...

4

2 回答 2

0

我没有收到任何错误,这意味着我连接到数据库和表

这并不意味着。

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.

还要删除所有 @ 并对这些函数返回的内容进行一些错误检查。

于 2013-10-17T07:19:36.510 回答
0

请如下更改以下行。

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

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

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

 mysqli_select_db($conn, $dbnm)
于 2013-10-17T07:23:56.893 回答