Question

I'm trying to neatly organize data from my MySQL database into tables. I'm almost there, but experiencing some troubles with my use of a while loop.

Posting my simple code:

<link type="text/css" rel="stylesheet" href="style.css"/>

<?PHP 
$connect = mysql_connect("localhost","root","");

mysql_select_db("users");
$query = mysql_query("SELECT * FROM users");
    WHILE($rows = mysql_fetch_array($query)):

        $username = $rows['username'];
        $password = $rows['password'];
        $email = $rows['email'];

    echo "
    <table>
    <thead>
    <th>Username</th>
    <th>Password</th>
    <th>Email Adress</th>
    </thead>
    <tr>
    <td>$username</td>
    <td>$password</td>
    <td>$email</td>
    ";
    endwhile;
?>

This gives me an output of something like this:

Username    Password    Email Adress

test           123  test@test.no

Username    Password    Email Adress

testies 123 testies@test.no

Username    Password    Email Adress

tested  12345   tested@test.no

The obvious problem here is that my headers are printed inside the while loop, and thus I get new headers for every entry. This looks really stupid though, and I would prefer to have the headers only appear once. Any ideas on how to do this?

I have tried to start the table with headers before the while loop, but then the later <td>s wont correspond with the headers width, since the program doesn't recognize them as the same table, and thus not needing to match.

Was it helpful?

Solution

Use this

<link type="text/css" rel="stylesheet" href="style.css"/>

<?PHP 
$connect = mysql_connect("localhost","root","");

mysql_select_db("users"); ?>
<table>
    <thead>
    <th>Username</th>
    <th>Password</th>
    <th>Email Adress</th>
    </thead><?php 

$query = mysql_query("SELECT * FROM users");
    WHILE($rows = mysql_fetch_array($query)) { ?>                    
    <tr>
    <td><?php echo $rows['username']; ?></td>
    <td><?php echo $rows['password']; ?></td>
    <td><?php echo $rows['email']; ?></td>
    </tr>
<?php } ?>    
?>

OTHER TIPS

change your code as follows.

<?PHP 
$connect = mysql_connect("localhost","root","");

mysql_select_db("users");
$query = mysql_query("SELECT * FROM users");
  echo "
    <table>
    <thead>
    <th>Username</th>
    <th>Password</th>
    <th>Email Adress</th>
    </thead>";
WHILE($rows = mysql_fetch_array($query)):
        $username = $rows['username'];
        $password = $rows['password'];
        $email = $rows['email'];


echo"<tr>
    <td>$username</td>
    <td>$password</td>
    <td>$email</td>
    ";
    endwhile;
?>

print the headers before the loop.

<table>
<thead>
<tr>
<th>Username</th>
<th>Password</th>
<th>Email Adress</th>
</tr>
</thead>
<?php 
$connect = mysql_connect("localhost","root","");

mysql_select_db("users");
$query = mysql_query("SELECT * FROM users");
    WHILE($rows = mysql_fetch_array($query)):

        $username = $rows['username'];
        $password = $rows['password'];
        $email = $rows['email'];

        echo "
        <tr>
        <td>$username</td>
        <td>$password</td>
        <td>$email</td>
        </tr>
        ";
    endwhile;
?>
</table>

Try this:

<link type="text/css" rel="stylesheet" href="style.css"/>

<?php 
$connect = mysql_connect("localhost","root","");
mysql_select_db("users");
$query = mysql_query("SELECT * FROM users");
?>
    <table>
    <thead>
    <th>Username</th>
    <th>Password</th>
    <th>Email Adress</th>
    </thead>";
<?php
WHILE($rows = mysql_fetch_array($query)):

    $username = $rows['username'];
    $password = $rows['password'];
    $email = $rows['email'];
echo "<tr>
<td>$username</td>
<td>$password</td>
<td>$email</td>
";
endwhile;
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top