TszKin Anthony
TszKin Anthony

Reputation: 15

PHP echo-ing a block of HTML but receiving unexpected result

   <div class="content">
        <h1>User Information</h1>
        <p>hi</p>

        <?php
            session_start();
            require_once 'dbconnect.php';
            $query = $DBcon->query("SELECT username, email, password, admin FROM tbl_users ");
            while($row=$query->fetch_array()){
                echo'
                    <div id="box">
                        <div class="box-top">$row['username']</div>
                        <div class="box-panel">
                            <p>$row['email']</p>
                            <p>$row['password']</p>
                            <p>$row['admin']</p>
                        </div>
                    </div>
                ';
            }
        ?>
    </div>

This is my code to echo a block of html.

What i am doing is that i am trying to connect this php to mysql database and print out the record in appropriate place. The dbconnect.php is for database connection which has been checked to be working fine.

I am getting this in my localhost website (I dismissed all css style and other appearance setting here).

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

User Information

hi

query("SELECT username, email, password, admin FROM tbl_users "); while($row=$query->fetch_array()){ echo <<< EOT $row['username'] $row['email']

$row['password']

$row['admin']

EOT; } ?>

Upvotes: 0

Views: 122

Answers (4)

Stuart
Stuart

Reputation: 6785

You need to use double quoted strings if PHP variables are to be interpolated.

Upvotes: 0

Antony Thompson
Antony Thompson

Reputation: 1638

You can't echo like that. Try this:

<?php
while($row=$query->fetch_array()){
    ?>
    <div id="box">
        <div class="box-top"><?php echo $row['username']; ?></div>
        <div class="box-panel">
            <p><?php echo $row['email']; ?></p>
            <p><?php echo $row['password']; ?></p>
            <p><?php echo $row['admin']; ?></p>
        </div>
    </div>
    <?php 
}

Upvotes: 1

Blinkydamo
Blinkydamo

Reputation: 1584

while( $row=$query->fetch_array() ) { ?>
<div id="box">
  <div class="box-top"><?php echo $row['username']; ?></div>
  <div class="box-panel">
    <p><?php echo $row['email']; ?></p>
    <p><?php echo $row['password']; ?></p>
    <p><?php echo $row['admin']; ?></p>
  </div>
</div>
<?php } ?>

Try this, it is a little cleaner and doesn't have issues with the quotes.

Upvotes: 0

Koen Hollander
Koen Hollander

Reputation: 1711

You don't use the quotes right.

Try the following:

<div class="content">
        <h1>User Information</h1>
        <p>hi</p>

        <?php
            session_start();
            require_once 'dbconnect.php';
            $query = $DBcon->query("SELECT username, email, password, admin FROM tbl_users");
            while($row=$query->fetch_array()){
                echo"
                    <div id='box'>
                        <div class='box-top'>".$row['username']."</div>
                        <div class='box-panel'>
                            <p>".$row['email']."</p>
                            <p>".$row['password']."</p>
                            <p>".$row['admin']."</p>
                        </div>
                    </div>
                ";
            }
        ?>
    </div>

Upvotes: 0

Related Questions