TszKin Anthony TszKin Anthony - 10 days ago 5
PHP Question

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; } ?>

Answer

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 
}
Comments