Page 1 of 1

user display problem

Posted: Mon May 19, 2008 11:15 am
by S A N T A
ok so i am attempting to create a user display panel that shows all the registered users.
here is the code i tried:

Code: Select all

<?php
define ( 'DB_USER', '****' );
define ( 'DB_PASS', '****' );
define ( 'DB_DB', '******' );
define ( 'DB_HOST', '******' );
mysql_connect(DB_HOST,DB_USER,DB_PASS) or die("Error connecting to Database! Please Try again." . mysql_error());
mysql_select_db(DB_DB) or die("Cannot select database! Please Try again." . mysql_error());
 
echo "Current users: "
?>
<br /><br /><br />
 
 
<?php
 
$sql = "SELECT * FROM `users` WHERE `username` AND `email`";
$result = mysql_query($sql)or die(mysql_error());
$row = mysql_fetch_assoc($result);
echo "<h2>" . $row['user'] . "</h2><br />";
echo "<h2>" . $row['email'] . "</h2><br />";
?>


i know this is probably completely wrong i just cannot get it to post the user names

thanks in advance

Re: user display problem

Posted: Mon May 19, 2008 11:31 am
by N1gel
I think you code should look something more like this

Code: Select all

 
<?php
  
 $sql = "SELECT * FROM `users`";
 $result = mysql_query($sql)or die(mysql_error());
 while($row = mysql_fetch_assoc($result))
 {
 echo "<h2>" . $row['user'] . "</h2><br />";
 echo "<h2>" . $row['email'] . "</h2><br />";
 }
?>
 

Re: user display problem

Posted: Mon May 19, 2008 11:46 am
by S A N T A
that works thanks.

how would i make each one display in a table row?

Re: user display problem

Posted: Tue May 20, 2008 6:01 am
by N1gel
This should put your results in a table.

I've added mysql_num_rows so that it won't display a blank table if no results are returned.

Code: Select all

 
<?php
  
$sql = "SELECT * FROM `users`";
$result = mysql_query($sql)or die(mysql_error());
 
if(mysql_num_rows($result) > 0)
{
    echo "<table border='1'>";
    echo "<tr><th>User</th><th>E-Mail</th></tr>";
    while($row = mysql_fetch_assoc($result))
    {
         echo "<tr>";
         echo "<td>".$row['user']."</td>";
         echo "<td>".$row['email']."</td>";
         echo "<tr/>";
    }
    echo "</table>";
}
?>