[PHP] Why will my value not display?

Blasteh

big tits
Apr 3, 2013
1,156
521
I'm adding some PHP onto my property management company's website but I cannot get the values to display from the database.

I added the PHP function below and only added the beds value from the database for this example. It comes back as nothing. For some reason, I cannot get any values from the database. I'm pretty stuck on why I can't get values. Any help is appreciated.

You must be registered for see images attach


Database Connection:
Code:
<?php
$mysqli = mysqli_connect("localhost","USER","PW","DB");

// Check connection
if ($mysqli -> connect_errno) {
  echo "Failed to connect: " . $mysqli -> connect_error;
  exit();
}

?>

PHP:
PHP:
                                <?php
                                $GetProperty = mysqli_query("SELECT id,img,beds FROM properties WHERE id > 0 ORDER by id");
                                    echo'
                                    <div class="listing-item">
                                        <a href="demo-real-estate-properties-detail.html" class="text-decoration-none">
                                            <div class="thumb-info thumb-info-lighten border">
                                                <div class="thumb-info-wrapper m-0">
                                                    <img src="img/demos/real-estate/listings/listing-1.jpg" class="img-fluid" alt="">
                                                    <div class="thumb-info-listing-type bg-color-secondary text-uppercase text-color-light font-weight-semibold p-1 pl-3 pr-3">
                                                        for sale
                                                    </div>
                                                </div>
                                                <div class="thumb-info-price bg-color-primary text-color-light text-4 p-2 pl-4 pr-4">
                                                    $ 530,000
                                                    <i class="fas fa-caret-right text-color-secondary float-right"></i>
                                                </div>
                                                <div class="custom-thumb-info-title b-normal p-4">
                                                    <div class="thumb-info-inner text-3">South Miami</div>
                                                    <ul class="accommodations text-uppercase font-weight-bold p-0 mb-0 text-2">
                                                        <li>
                                                            <span class="accomodation-title">
                                                                Beds:
                                                            <span class="accomodation-value custom-color-1">
                                                            </span>
                                                                '.$GetProperty['beds'].'
                                                            </span>
                                                        </li>
                                                        <li>
                                                            <span class="accomodation-title">
                                                                Baths:
                                                            </span>
                                                            <span class="accomodation-value custom-color-1">
                                                                2
                                                            </span>
                                                        </li>
                                                        <li>
                                                            <span class="accomodation-title">
                                                                Sq Ft:
                                                            </span>
                                                            <span class="accomodation-value custom-color-1">
                                                                500
                                                            </span>
                                                        </li>
                                                    </ul>
                                                </div>
                                            </div>
                                        </a>
                                    </div>';
                                 ?>
 

TesoMayn

Boredom, it vexes me.
Oct 30, 2011
1,482
1,482
In my opinion, it's a poor habit to put your entire site inside an echo.

As for your PHP a simple fix is to replace
PHP:
$GetProperty = mysqli_query("SELECT id,img,beds FROM properties WHERE id > 0 ORDER by id");
with
PHP:
$GetProperties = mysqli_query($mysqli, "SELECT id,img,beds FROM properties WHERE id > 0 ORDER by id");
$GetProperty = mysqli_fetch_assoc($GetProperties);
Or if you wish to do it in one line:
PHP:
$GetProperty = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT id,img,beds FROM properties WHERE id > 0 ORDER by id"));
You should also consider using OOP instead of procedural, or even switch to a framework such as Laravel
 

Blasteh

big tits
Apr 3, 2013
1,156
521
In my opinion, it's a poor habit to put your entire site inside an echo.

As for your PHP a simple fix is to replace
PHP:
$GetProperty = mysqli_query("SELECT id,img,beds FROM properties WHERE id > 0 ORDER by id");
with
PHP:
$GetProperties = mysqli_query($mysqli, "SELECT id,img,beds FROM properties WHERE id > 0 ORDER by id");
$GetProperty = mysqli_fetch_assoc($GetProperties);
Or if you wish to do it in one line:
PHP:
$GetProperty = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT id,img,beds FROM properties WHERE id > 0 ORDER by id"));
You should also consider using OOP instead of procedural, or even switch to a framework such as Laravel
Thanks Teso!

Another question, how do I make it so it continues to display values from the database? I have 2 rows, the exact same but only the first one shows. I changed the query to this for now:
PHP:
$GetProperty = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT * FROM properties WHERE id > 0 ORDER by id"));

You must be registered for see images attach
You must be registered for see images attach


Edit: Added my gross PHP for reference ( )
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
Instead of $GetProperty = mysqli_fetch_assoc, you would do a while loop on the mysqli_fetch_assoc

So something like:
PHP:
while ($property = mysqli_fetch_assoc($GetProperties)) {
  // your markup here
}

I’m on phone so it’s harder to type
 

TesoMayn

Boredom, it vexes me.
Oct 30, 2011
1,482
1,482
You have to loop it in order to display multiple results.

I rewrote it for you, taking the HTML out of an echo

PHP:
<?php
$mysqli = new mysqli("localhost","root","password","database");
if ($mysqli->connect_errno) {
    echo $mysqli->connect_error;
    exit();
}

if ( $res = $mysqli->query("SELECT id,img,beds FROM properties ORDER by id") ) {
    while ($property = $res->fetch_assoc()) {
?>
<div class="listing-item">
    <a href="demo-real-estate-properties-detail.html" class="text-decoration-none">
        <div class="thumb-info thumb-info-lighten border">
            <div class="thumb-info-wrapper m-0">
                <img src="img/demos/real-estate/listings/listing-1.jpg" class="img-fluid" alt="">
                <div class="thumb-info-listing-type bg-color-secondary text-uppercase text-color-light font-weight-semibold p-1 pl-3 pr-3">
                    for sale
                </div>
            </div>
            <div class="thumb-info-price bg-color-primary text-color-light text-4 p-2 pl-4 pr-4">
                $ 530,000
                <i class="fas fa-caret-right text-color-secondary float-right"></i>
            </div>
            <div class="custom-thumb-info-title b-normal p-4">
                <div class="thumb-info-inner text-3">South Miami</div>
                <ul class="accommodations text-uppercase font-weight-bold p-0 mb-0 text-2">
                    <li>
                        <span class="accomodation-title">
                            Beds:
                            <span class="accomodation-value custom-color-1"></span>
                            <?=$property->beds?>
                        </span>
                    </li>
                    <li>
                        <span class="accomodation-title">
                            Baths:
                        </span>
                        <span class="accomodation-value custom-color-1">
                            2
                        </span>
                    </li>
                    <li>
                        <span class="accomodation-title">
                            Sq Ft:
                        </span>
                        <span class="accomodation-value custom-color-1">
                            500
                        </span>
                    </li>
                </ul>
            </div>
        </div>
    </a>
</div>
<?php
    }
    $res->free_result();
}
$mysqli->close()
?>
 

Blasteh

big tits
Apr 3, 2013
1,156
521
You have to loop it in order to display multiple results.

I rewrote it for you, taking the HTML out of an echo

PHP:
<?php
$mysqli = new mysqli("localhost","root","password","database");
if ($mysqli->connect_errno) {
    echo $mysqli->connect_error;
    exit();
}

if ( $res = $mysqli->query("SELECT id,img,beds FROM properties ORDER by id") ) {
    while ($property = $res->fetch_assoc()) {
?>
<div class="listing-item">
    <a href="demo-real-estate-properties-detail.html" class="text-decoration-none">
        <div class="thumb-info thumb-info-lighten border">
            <div class="thumb-info-wrapper m-0">
                <img src="img/demos/real-estate/listings/listing-1.jpg" class="img-fluid" alt="">
                <div class="thumb-info-listing-type bg-color-secondary text-uppercase text-color-light font-weight-semibold p-1 pl-3 pr-3">
                    for sale
                </div>
            </div>
            <div class="thumb-info-price bg-color-primary text-color-light text-4 p-2 pl-4 pr-4">
                $ 530,000
                <i class="fas fa-caret-right text-color-secondary float-right"></i>
            </div>
            <div class="custom-thumb-info-title b-normal p-4">
                <div class="thumb-info-inner text-3">South Miami</div>
                <ul class="accommodations text-uppercase font-weight-bold p-0 mb-0 text-2">
                    <li>
                        <span class="accomodation-title">
                            Beds:
                            <span class="accomodation-value custom-color-1"></span>
                            <?=$property->beds?>
                        </span>
                    </li>
                    <li>
                        <span class="accomodation-title">
                            Baths:
                        </span>
                        <span class="accomodation-value custom-color-1">
                            2
                        </span>
                    </li>
                    <li>
                        <span class="accomodation-title">
                            Sq Ft:
                        </span>
                        <span class="accomodation-value custom-color-1">
                            500
                        </span>
                    </li>
                </ul>
            </div>
        </div>
    </a>
</div>
<?php
    }
    $res->free_result();
}
$mysqli->close()
?>
Thanks again, Teso. For some reason, values are now back to not displaying anymore. I had this problem when I used "new mysqli" before. Do I need to upgrade my version of PHP or something? I'm using cPanel at the moment.
 
Last edited:

TesoMayn

Boredom, it vexes me.
Oct 30, 2011
1,482
1,482
Does it give an error?

If it's not returning any value, it should be throwing an error, if not you might have to enable it by putting this at the top of the file
PHP:
ini_set('display_errors', 1);
error_reporting(E_ALL);
 

Blasteh

big tits
Apr 3, 2013
1,156
521
Does it give an error?

If it's not returning any value, it should be throwing an error, if not you might have to enable it by putting this at the top of the file
PHP:
ini_set('display_errors', 1);
error_reporting(E_ALL);
Yea, error shows up as:

Code:
Trying to get property 'beds' of non-object in <file path>
 

Blasteh

big tits
Apr 3, 2013
1,156
521
Oh that's on me, I did fetch_assoc() instead of fetch_object()


Just change assoc to object and it'll work
Or if you don't want to use objects, change
$property->beds
to
$property['beds']
Works.

What is the difference between fetch_assoc and fetch_object? Just out of curiosity.
 

Users who are viewing this thread

Top