0

我正在尝试解决这个问题。但是做不到。请帮忙

我正在尝试查看数据库中的所有记录,但得到“致命错误:调用非对象上的成员函数 query() ....”及其指向

if ($result = $mysqli->query("SELECT ProductName, Description, Price, Size FROM Products ORDER BY ProductID"))

这是我的完整代码

<?php

                        include('db_connect.php');

**$mysqli = new mysqli("localhost", "netelmbn", "password", "netelmbn");
                       if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}**


                        if ($result = $mysqli->query("SELECT ProductName, Description, Price, Size FROM Products ORDER BY ProductID"))
                        {

                                if ($result->num_rows > 0)
                                {

                                        echo "<table border='1' cellpadding='10'>";


                                        echo "<tr><th>ProductID</th><th>ProductName</th><th>Description</th><th>Price</th><th>Size</th></tr>";

                                        while ($row = $result_fetch_object())
                                        {

                                              echo "<tr>";
                                                echo "<td>" . $row->ProductID . "</td>";
                                                echo "<td>" . $row->ProductName . "</td>";
                                                echo "<td>" . $row->Description . "</td>";
                                                echo "<td>" . $row->Price . "</td>";
                                                echo "<td>" . $row->Size . "</td>";
                                                echo "<td><a href='records.php?ProductID=" . $row->ProductID . "'>Edit</a></td>";
                                                echo "<td><a href='delete.php?ProductID=" . $row->ProductID . "'>Delete</a></td>";
                                                echo "</tr>";
                                        }
                                        echo "</table>";
                                }
                                else
                                {
                                        echo "No results to display!";
                                }
                        }
                        else
                        {
                                echo "Error: " . $mysqli->error;
                        }


                        $mysqli->close();

                ?>
4

2 回答 2

1

你需要先实例化你的 $mysqli obect。您也没有在任何地方打开数据库。

你至少需要这样的东西:

$mysqli = new ClassName( /*some parameters here*/ );

$mysqli->database_open( /* some parameters here */); // or something like this, look at the clas definition
于 2012-05-14T07:03:28.393 回答
1

你必须有一些数据库类,检查你的代码。

$mysqli = new Database();
$mysqli->connect();

在执行查询之前:

$result = $mysqli->query("SELECT ProductName, Description, Price, Size FROM Products ORDER BY ProductID");

如您所见,查询首先进行,然后是连接资源。>> 所以,先连接。

于 2012-05-14T07:25:27.753 回答