PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
S A N T A
Forum Newbie
Posts: 10 Joined: Sat May 10, 2008 4:43 pm
Post
by S A N T A » Mon May 19, 2008 11:15 am
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
N1gel
Forum Commoner
Posts: 95 Joined: Sun Apr 30, 2006 12:01 pm
Post
by N1gel » Mon May 19, 2008 11:31 am
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 />";
}
?>
S A N T A
Forum Newbie
Posts: 10 Joined: Sat May 10, 2008 4:43 pm
Post
by S A N T A » Mon May 19, 2008 11:46 am
that works thanks.
how would i make each one display in a table row?
N1gel
Forum Commoner
Posts: 95 Joined: Sun Apr 30, 2006 12:01 pm
Post
by N1gel » Tue May 20, 2008 6:01 am
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>";
}
?>