PHP从数据库中取出

时间:2015-05-14 21:56:59

标签: php mysql

我非常擅长PHP,我只需要有人为我提供答案,因为我的老师毫无价值,而且我愿意向这里的任何人学习。

简单地说,我有一个上传表单,用户可以上传房子的图片,以及有关房价的信息。

我正在使用XAMPP和PHPMYADMIN。我有一个简单的数据库,有一个表准备好了详细信息。当我上传房子时,所有信息都显示在数据库中。

问题是,获取数据库的信息。我一直收到错误说:

  

警告:mysqli_fetch_assoc()期望参数1为mysqli_result,   在E:\ xampp-portable-win32-5.6.3-0-VC11中给出的布尔值   (1)第107行\ xampp \ htdocs \ EstateAgent \ houses.php连接失败:

我不知道如何解决这个问题,但我真的希望得到你们的一些帮助,因为就像我说的那样,我的老师实际上没有教给我们任何东西。

基本上,这就是我的代码:

<form name="new-house-form" method="post" action="upload_house.php" enctype="multipart/form-data">
    <label for="housepic">House Image: </label>
        <input type="file" name="housepic" id="housepic"><br>
        <label for="houseprice">House Price: </label>
        <input type="text" name="houseprice" id="houseprice"><br>
        <label for="housetype">House Type: </label>
    <input type="text" name="housetype" id="housetype"><br>
    <label for="houseloc">House Location: </label>
    <input type="text" name="houseloc" id="houseloc"><br>
    <label for="housedesc">House Description: </label>
    <input type="text" name="housedesc" id="housedesc"><br>
    <input type="submit" value="Upload">
</form>
    <div id="content">
        <h2>Our Houses</h2>
        <div class="housepost">
            <img src="img/houses/house_01.jpg">
            <h2>£350,000</h2>
            <p>2 bedroom detached house for sale</p>
            <p>Deanfield Avenue, Henley-on-Thames</p>
            <p>Set in the heart of Henley-on-Thames and just a short walk from Henley train station is this rarely available and spacious three bedroom apartment. Offered to the market with no onward chain the property benefits from off road parking.</p>
        </div>
        <div class="housepost">
            <img src="img/houses/house_02.jpg">
            <h2>£475,000</h2>
            <p>2 bedroom detached bungalow for sale</p>
            <p>Fair Mile, Henley-on-Thames</p>
            <p>Set in the heart of the town centre in a quiet backwater this delightful single storey detached home is rarely available and well presented.</p>
        </div>
        <div class="housepost">
            <img src="img/houses/house_03.jpg">
            <h2>£600,000</h2>
            <p>3 bedroom cottage for sale</p>
            <p>Remenham Row, Henley-on-Thames</p>
            <p>The English Courtyard Association and The Beechcroft Trust - synonymous with the very best in retirement housing since the 1980s. An extremely attractive three-bedroom cottage with landscaped riverside gardens in this much sought after location.</p>
        </div>
        <?php

    $servername="localhost";
    $username="root";
    $password="";
    $dbname="content_management"; 
    $tbl_name="houses";

        require_once("db_const.php");
    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    # check connection
    if ($mysqli->connect_errno) {
        echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
        exit();
    }

    $sql = "SELECT * FROM $tbl_name";
    $result= $mysqli->query($sql);

    while ( $row = mysqli_fetch_assoc($result) ) {
        echo "<img src='" . $row['picture'] . "'>";
            echo $row['price'];
            echo $row['type'];
        echo $row['location'];
        echo $row['description'];
    }
       mysqli_close($mysqli);

?>

已经有3个房屋已经用HTML进行了硬编码,暂时忘记这些房屋。这是我关心的PHP。第107行是:

while ( $row = mysqli_fetch_assoc($result) ) {

答案可能非常简单,这可能是重复的,但我真的需要帮助。阅读其他答案对我没有帮助,因为我对PHP语法非常不满意。 v.v

3 个答案:

答案 0 :(得分:2)

错误几乎说明了:

  

警告:mysqli_fetch_assoc()期望参数1为mysqli_result,给定布尔值

这意味着$result= $mysqli->query($sql);返回false,而不是有效结果。我注意到你有点奇怪地定义了你的数据库凭证:

$servername="localhost";
$username="root";
$password="";
$dbname="content_management"; 
$tbl_name="houses";

require_once("db_const.php");
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

您首先使用凭据定义变量,但是您没有使用这些变量,但是包含一个我认为凭证被声明为常量的文件。

您可以通过检查$resultfalse是否为$mysqli->error来查看实际错误,如果是,则打印出measureTimeExecution

答案 1 :(得分:0)

Schlaus的答案是迄今为止最正确的答案。我运行你的代码,我的机器尊重的值和查询工作得很好。错误必须在db_const.php文件中,因为这是分配访问数据库的值的地方。

如其他两个答案所述,该行不是问题。

以下代码可以解决这个问题:

$servername="localhost";
$username="root";
$password="";
$dbname="content_management"; 
$tbl_name="houses";

// require_once("db_const.php");
$mysqli = new mysqli($servername, $username, $password, $dbname);
// check connection
if ($mysqli->connect_errno) {
    echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
    exit();
}

$sql = "SELECT * FROM $tbl_name";
if (!$result = $mysqli->query($sql)) {
    echo "Error is: " . $mysqli->error;
} else {
    while ( $row = mysqli_fetch_assoc($result) ) {
        echo "<img src='" . $row['picture'] . "'>";
        echo $row['price'];
        echo $row['type'];
        echo $row['location'];
        echo $row['description'];
    }
}

mysqli_close($mysqli);

答案 2 :(得分:-1)

试试这个?

while ( $row = $result->fetch_assoc() ) {