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
https://forums.phpfreaks.com/topic/92373-getting-data-from-mysql/
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
}
}
?>

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.