Forum Moderators: coopster

Message Too Old, No Replies

displaying results in a table from mysql db..

         

firemaster

2:50 am on Jul 12, 2003 (gmt 0)

10+ Year Member



I tried to write a code to create a table with data from a mysql db.
here is the code I wrote:

<?php
session_start();
?>
<html>
<HEAD>
<TITLE>MEMBER LIST</TITLE>
<link href="../css/red.css" rel="stylesheet" type="text/css">
</HEAD>
<BODY class="body">
<h5 align="center">Members List</h5>
<table class="body" border="1" bgcolor="#CCCCCC" bordercolor="#000000" align="center">
<tr bgcolor="#FF0000">
<td><font color="#000000"><b>Username</b></font></td>
<td><font color="#000000"><b>Status</b></font></td>
<td><font color="#000000"><b>Gender</b></font></td>
<td><font color="#000000"><b>Website</b></font></td>
<td><font color="#000000"><b>More Info</b></font></td>
</tr>
<?php
include("db_fns.php"); // file to connect to DB
$result = "SELECT `ID` FROM members";
$ms_query = mysql_query($result, $db_conn) or die (mysql_errno().": ".mysql_error()."<BR>");
$count = mysql_num_rows($ms_query);
$id_num = 0;
for ($id_num > 0; $id_num >= $count; $id_num++)
{
$user_var = "SELECT * FROM members WHERE ID = '$id_num'";
$user_var_query = mysql_query($user_var, $db_conn) or die (mysql_errno().": ".mysql_error()."<BR>");
while($row = mysql_fetch_array($user_var_query)) {
$user_name = $row['name'];
$user_email = $row['email'];
$user_bio = $row['bio'];
$user_aim = $row['aim'];
$user_gender = $row['gender'];
$user_website = $row['website'];
$user_bday = $row['birthdate'];
$user_status = $row['status'];
if($user_status == "")
{
$user_status = "Member";
} else {
$user_status = $row['status'];
}
}
print"<tr>
<td>$user_name</td>
<td>$$user_status</td>
<td>$user_gender</td>
<td>$user_website</td>
<td>More Info</td>
</tr>"; // row printed with member info
}
?>
</table>
</body>
</html>

All it shows is the HTML part at the to before it gets to the PHP. Please help me to get this work, what more info is needed to help. I printed the $count, 46. I know thier are some 'useless' parts in the array but I think I might use them in it later (the more info).
thanks,
-Mike

Knowles

3:28 am on Jul 12, 2003 (gmt 0)

10+ Year Member



Mike,

It looks to me like your making it a little harder on yourself then you need to be. I am tired so I may be reading it wrong. But why not restructor it more like this:

$result = "SELECT * FROM members";
Left out stuff here!
while($row = mysql_fetch_array($user_var_query))
{
<tr>
<td>$row['name']</td>
<td>$row['status']</td>
<td>$row['gender']</td>
<td>$row['website']</td>
</tr>
}

There may be some major issues with this code... which I think there are it just doesnt seem quite right either but its a start. If I remember Ill look at it again in the morning after I have rested some.

firemaster

3:51 am on Jul 12, 2003 (gmt 0)

10+ Year Member



You are so very right.. I haven't used a DB all too much. Overlooking what you said, and fixing it, here is the final, working code:

<?php
session_start();
?>
<html>
<HEAD>
<TITLE>MEMBER LIST</TITLE>
<link href="../css/red.css" rel="stylesheet" type="text/css">
</HEAD>
<BODY class="body">
<h5 align="center">Members List</h5>
<table class="body" border="1" bgcolor="#CCCCCC" bordercolor="#000000" align="center">
<tr bgcolor="#FF0000">
<td><font color="#000000"><b>Username</b></font></td>
<td><font color="#000000"><b>Status</b></font></td>
<td><font color="#000000"><b>Gender</b></font></td>
<td><font color="#000000"><b>Website</b></font></td>
<td><font color="#000000"><b>More Info</b></font></td>
</tr>
<?php
include("db_fns.php"); // file to connect to DB
$user_var = "SELECT * FROM members";
$user_var_query = mysql_query($user_var, $db_conn) or die (mysql_errno().": ".mysql_error()."<BR>");
while($row = mysql_fetch_array($user_var_query)) {
$user_name = $row['name'];
$user_email = $row['email'];
$user_bio = $row['bio'];
$user_aim = $row['aim'];
$user_gender = $row['gender'];
$user_website = $row['website'];
$user_bday = $row['birthdate'];
$user_status = $row['status'];
if($user_status == "")
{
$user_status = "Member";
} else {
$user_status = $row['status'];
}
print"<tr>
<td>$user_name</td>
<td>$user_status</td>
<td>$user_gender</td>
<td>$user_website</td>
<td>More Info</td>
</tr>";
}
?>
</table>
</body>
</html>

if I did not make $row['colum'] equal to a var, then nothing would show up. Thanks,
-Mike

Knowles

11:51 am on Jul 12, 2003 (gmt 0)

10+ Year Member



Glad you figured it out... Another thing to remember is to only select the stuff your using in. YOu are selecting ALL but there are 3 or so things your not using. No need to select those.

Yeah what I put up there is kind of nasty I didnt think it looked right last night but couldnt put my finger on it.

firemaster

1:34 pm on Jul 12, 2003 (gmt 0)

10+ Year Member



I am selecting all because I am debating which ones I want to use, the more info is going to be more info.
-Mike