-1

I was try do view in php. But first data now showing in output. Remaining data displayed. how can i get all data in table.

<html>
<body>
<?php
 include('connect.php');
  $select=mysql_query("SELECT id,username FROM user order by id");
 $i=1;
  while($userrow=mysql_fetch_array($select))
  {
    $id=$userrow['id'];
    $name=$userrow['username'];
      ?>
<table width="600" border="1"  cellpadding="1" cellspaceing="1" >  
<tr >
<th class="active">ID</th>
<th class="active">Name</th>
<tr>
<?php 
while ($employee=mysql_fetch_assoc($select))
{
    echo "<tr>";
    echo "<td>".$employee['id']."</td>";
    echo "<td>".$employee['username']."</td>";
 } 
  }
?>
</table>
</body>
</html>
  • 3
    Please dont use the `mysql_` database extension, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the `PDO` or `mysqli_` database extensions, [and here is some help to decide which to use](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – RiggsFolly Jan 13 '16 at 12:49
  • Close the `` tag with a `` inside the loop – RiggsFolly Jan 13 '16 at 12:55
  • Oh dear, on closer inspection its just a mess! Start again and this time think about what you are trying to do – RiggsFolly Jan 13 '16 at 13:04
  • `cellspaceing`? that's invalid syntax. – Funk Forty Niner Jan 13 '16 at 13:05
  • Nobody's paying attention to us Smokey @RiggsFolly why waste your/our breath. – Funk Forty Niner Jan 13 '16 at 13:23

2 Answers2

1

There are multiple issues:

  • Table rows had not been closed (</tr>)
  • Usage of mysql_* functions is deprecated in PHP 5 and removed in PHP 7; if you're writing new code, you should not be using mysql_* functions. Instead, use mysqli_* functions or the PDO library.
  • There were multiple for-loops to get records, but it would create new tables each iteration.
  • The table attribute "cellspacing" was misspelled.
  • The results object where they are stored can either be a resource or false.

Given these, I've made the following changes that will fix the above problems:

<?php

// I've commented out the connect.php file inclusion to highlight the change to how
// you'd connect to the MySQL database using mysqli_* instead of mysql_*. I'm guessing
// as to the contents of connect.php, but this should still highlight what needs to be
// done

// include('connect.php');
$db_connection = mysqli_connect('host', 'user', 'pass', 'database_name');
$employee_results = mysqli_query($db_connection, "SELECT id,username FROM user ORDER BY id");
?>

<html>
<body>
<table width="600" border="1" cellpadding="1" cellspacing="1">
    <tr>
        <th class="active">ID</th>
        <th class="active">Name</th>
    </tr>
    <?php if ($employee_results === false): ?>
        <tr>
            <td colspan="2">No users</td>
        </tr>
    <?php else: ?>
        <?php while ($employee = mysqli_fetch_assoc($employee_results)): ?>
            <tr>
                <td><?= $employee['id'] ?></td>
                <td><?= $employee['username'] ?></td>
            </tr>
        <?php endwhile ?>
    <?php endif ?>
</table>
</body>
</html>
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
0
<html>
<body>
<?php
 include('connect.php');
  $select=mysql_query("SELECT id,username FROM user order by id");
 $i=1; ?> 
    <table width="600" border="1"  cellpadding="1" cellspaceing="1" > 
        <tr>
<th class="active">ID</th>
<th class="active">Name</th></tr>
<?php   while($userrow=mysql_fetch_array($select))
  {
    $id=$userrow['id'];
    $name=$userrow['username'];
      ?>

<?php 
    echo "<tr>";
    echo "<td>".$id."</td>";
    echo "<td>".$name."</td></tr>";
  }
?>
</table>
</body>
</html>`
Pritam Jinal
  • 121
  • 9
  • **Edit your original Answer** Dont add 2 answers – RiggsFolly Jan 13 '16 at 13:06
  • 1
    Why should the OP "try this"? A **good answer** will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO that may find this question and be reading your answer. – RiggsFolly Jan 13 '16 at 13:08
  • plus, you didn't fix OP's syntax error `cellspaceing`. best to catch all errors and to fix them for future visitors. – Funk Forty Niner Jan 13 '16 at 13:09