0

我正在尝试连接到数据库并在页面上显示信息。

这是模型

<?php
// model for the totals of the wreath orders
require_once("dbconnect.php");

// connect to database and check errors
@ $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

$connection_error = $dbc->connect_error;
if ($connection_error != null) {
  echo "<p>Error connectiong to database: $connection_error</p>";
  exit();
}
?>

这是我构造字符串并尝试连接并在屏幕上显示数据的页面中的代码。

<?php
require_once('model.php');

$query = "SELECT * FROM people WHERE";

if  (isset ($_POST['fname'])) {
    $fname = $_POST['fname'];
    $query = $query . " fname = '" . $fname . "' AND";
}
if (isset ($_POST['lname'])) {
    $lname = $_POST['lname'];
    $query = $query . " lname = '" . $lname . "' AND";
}

if (isset ($_POST['age'])) {
    $age = $_POST['age'];
    $query = $query . " age = '" . $age . "' AND";
}

if (isset ($_POST['city'])) {
    $city = $_POST['city'];
    $query = $query . " city = '" . $city . "' AND";
}

$query = rtrim($query, " AND");
include('header.php');
?>
<div id="header">
<h1><strong>This is the information you requested</h1></strong>
</div>
<div id="main">
<?php
$results = $dbc->query($query);
$row_count = $result->num_rows;

for ($i = 0; $i < $row_count; $i++) :
        $product = $result->fetch_assoc();
    echo $product['fname'] . " | " . $product['lname'] . " | " . $product['age'] . " | " . $product['city'] . ' <br />';
endfor;
?>
</div>
<?php
include('footer.php');

这是我得到的错误

Notice: Undefined variable: result in C:\Program Files\wamp\www\testwebpage\Model\getinformation.php on line 34

Trying to get property of non-object in C:\Program Files\wamp\www\testwebpage\Model\getinformation.php on line 34

我可以说我对此很陌生,只是不确定代码会有什么问题。

4

1 回答 1

0

您正在使用不同的变量名称。它应该$results代替$result.

将它们替换为:

$row_count = $results->num_rows; //line 34

$product = $results->fetch_assoc(); //line 37

那应该可以解决错误。

于 2013-07-19T16:26:56.787 回答