0

I'm really bad at PHP, and I just need someone to provide an answer for me because my teacher is worthless and I'm willing to learn from anyone here.

Simply, I have an upload form where the user can upload a picture of a house, and information on its price and what not.

I am using XAMPP and PHPMYADMIN. I have a simple database with a table ready for the details. When I upload the house, all the information is shown in the database fine.

The problem is, getting the information /out/ of the database. I keep getting an error saying:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in E:\xampp-portable-win32-5.6.3-0-VC11 (1)\xampp\htdocs\EstateAgent\houses.php on line 107 Connection failed:

I have no idea how to fix this, but I would really like some help from you guys, because like I said, my teacher literally taught us nothing.

Essentially, this is what my code looks like:

<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);

?>

There are 3 houses already hard coded in their with HTML, just forget about those for now. It's the PHP I am concerned with. Line 107 is the line that reads:

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

The answer is probably super simple, and this is probably a duplicate, but I just really need help. Reading other answers doesn't help me because I am really bad at PHP syntax. v.v

Arne Claassen
  • 13,700
  • 4
  • 61
  • 104
  • Did you spell the table name correctly in the PHP and the database? Try echoing `mysqli_error()` after the query. – Mike May 14 '15 at 22:00
  • ... and your teacher is getting you to use phpMyAdmin to learn MySQL with? – Mike May 14 '15 at 22:01
  • Yeah, they're both 'houses'. and yes, unfortunately. It's just this assignment, and then I can be finished with PHP and MySQL forever. He doesn't help, and a lot of the code I don't fully understand, it's just bits I copied from my own work in the lesson and tried to put together. – YouCan'tSeeMe May 14 '15 at 22:04

3 Answers3

2

The error pretty much says it:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given

It means $result= $mysqli->query($sql); is returning false, and not a valid result. I noticed you're defining your database credentials a bit weirdly:

$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);

You're first defining variables with the credentials, but then you're not using those, but including a file which I suppose has the credentials declared as constants.

You can see what the actual error is by checking whether $result is false, and if it is, then print out $mysqli->error.

Schlaus
  • 16,896
  • 10
  • 34
  • 63
  • Okay first of all, thank you, I didn't even notice that. I am actually deleting that 'Require_once' line and all that because it was an experiment, I'm just an 18 year old who has no idea how to code PHP. – YouCan'tSeeMe May 14 '15 at 22:08
  • @Nomadnomad don't worry about it, you learn by trying stuff out. If you remove the `require_once` remember to also change the constants in `new mysqli(...)` to the local variables you're assigning the credentials to. It's not a bad idea to use constants `include`d from another file. Just pick which ever way suits your needs. – Schlaus May 14 '15 at 22:11
0

Schlaus' answer is the most correct to date. I ran your code, with values that my machine respects and the query works just fine. The error must be in your db_const.php file, as this is where the values for accessing the database are being allocated.

It is not a problem with the line as stated in the other two answers.

The following code should do the trick:

$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);
GKnight
  • 274
  • 1
  • 9
  • Yeah, Schlauss was right, that stuff wasn't needed, and now all the stuff comes out of the database! it's awkwardly formatted though. Perhaps you guys could help me again real quick then this time. You see those three divs in HTML? well, I'd like my outputs from the database to be like those divs, except in PHP. So, they upload a house through the form, and when the page loads again, it comes out like those divs do. Is there a way to echo the rows and variables into its own divs so that it has the same format as those hardcoded ones? – YouCan'tSeeMe May 14 '15 at 22:20
-1

Try this?

while ( $row = $result->fetch_assoc() ) {
Kevin_Kinsey
  • 2,179
  • 1
  • 20
  • 23
  • 1
    That wont make a difference. `mysqli_fetch_assoc($result);` is a vlid procedural way to fetch the results. The problem is that `$result` is a boolean, not a result. – Schlaus May 14 '15 at 22:08