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

2
  • You have to escape the quotes. Your code is not valid Commented Nov 23, 2016 at 11:08
  • Try this way you have issue with quotes. Replace my echo code to yours - 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> "; Commented Nov 23, 2016 at 11:08

4 Answers 4

1

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 
}
Sign up to request clarification or add additional context in comments.

2 Comments

I thought I can echo a block of html like that way. Thanks a lot
Koen's answer would work too, just a matter of preference.
0

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>

1 Comment

What did you get?
0
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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.