我正在尝试解决此问题。但是无法做到。请帮忙
我正在尝试查看我的数据库中的所有记录,但得到“致命错误:在非对象上调用成员函数查询()....”并指向
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();
?>
答案 0 :(得分:1)
您需要先实例化$ mysqli对象。您还没有在任何地方打开数据库。
至少你需要这样的东西:
$mysqli = new ClassName( /*some parameters here*/ );
$mysqli->database_open( /* some parameters here */); // or something like this, look at the clas definition
答案 1 :(得分:1)
您必须拥有一些数据库类,请检查您的代码。
$mysqli = new Database();
$mysqli->connect();
在执行查询之前:
$result = $mysqli->query("SELECT ProductName, Description, Price, Size FROM Products ORDER BY ProductID");
如您所见,查询先行,然后是连接资源。 &GT;&GT;所以,先连接。