Jump to content

Getting Data From MySQL


andrew6607

Recommended Posts

Hello,

I can not figure out how to fix this problem. I am trying to get data from mysql. I have 3 records in the table but it will only display one. Below is the code.

 

<?php
$Host = "localhost";
$User = "root";
$Password = "******";
$DBName = "website";
$TableName = "members";

$Conn = mysql_connect ($Host, $User, $Password) or die ( "MySQL Error!" );

//now lets do the query
$Query = "SELECT * from $TableName";
$Result = mysql_db_query ($DBName, $Query, $Conn) or die ( "MySQL Error 2!" );

$data=mysql_fetch_assoc($Result);


?>
<table border="1" align="center">
     <tr>  
        <td>First Name</td><td>Last Name</td><td>Email</td><td>User Level</td><td>Delete</td><td>Edit User</td>
     </tr>
     <tr>
        <td><?php echo $data['fname']; ?></td><td><?php echo $data['lname']; ?></td><td><?php echo $data['email']; ?></td><td><?php echo $data['level']; ?></td><td><a href="delete.php?fname=<?php echo $data['fname']; ?>">Delete User</a></td><td><a href="edit.php?fname=<?php echo $data['fname']; ?>">Edit User</a></td>
     </tr>
</table>

Thank you ahead of time!

Link to comment
Share on other sites

use this

 

<?php
$Query = mysql_query("SELECT * from $TableName") or die (mysql_error());
if (mysql_num_rows($Query)) {
while ($data = mysql_fetch_array($Query)) {
?>
<table border="1" align="center">
     <tr>  
        <td>First Name</td><td>Last Name</td><td>Email</td><td>User Level</td><td>Delete</td><td>Edit User</td>
     </tr>
     <tr>
        <td><?php echo $data['fname']; ?></td><td><?php echo $data['lname']; ?></td><td><?php echo $data['email']; ?></td><td><?php echo $data['level']; ?></td><td><a href="delete.php?fname=<?php echo $data['fname']; ?>">Delete User</a></td><td><a href="edit.php?fname=<?php echo $data['fname']; ?>">Edit User</a></td>
     </tr>
</table>
<?php
}
}
?>

Link to comment
Share on other sites

You need to use a while loop, like this:

 

<?php
$Host = "localhost";
$User = "root";
$Password = "******";
$DBName = "website";
$TableName = "members";

$Conn = mysql_connect ($Host, $User, $Password) or die ( "MySQL Error!" );

//now lets do the query
$Query = "SELECT * from $TableName";
$Result = mysql_db_query ($DBName, $Query, $Conn) or die ( "MySQL Error 2!" );


?>
<table border="1" align="center">
     <tr>  
        <td>First Name</td><td>Last Name</td><td>Email</td><td>User Level</td><td>Delete</td><td>Edit User</td>
     </tr>
     
<?php

while ($data=mysql_fetch_assoc($Result)){
     echo '<tr>';
        echo "<td>{$data['fname']}</td><td>{$data['lname']}</td><td>{$data['email']}</td><td>{$data['level']}</td>
        <td><a href='delete.php?fname={$data['fname']}'>Delete User</a></td>
        <td><a href='edit.php?fname={$data['fname']}'>Edit User</a></td>";
     echo '</tr>';
}
     
?>
</table>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.