Jump to content

[SOLVED] I need a bit of help with this members list script....


bobleny

Recommended Posts

This script is supposed to list all the members in the "user" database. It is only showing the first row. It just keeps repeating the first row...

 

<?php
$connect = mysql_connect($database_hostname, $database_username, $database_password);
if(!$connect)
{
$_SESSION['error_message'] = mysql_error();
$_SESSION['error_location'] = "Page: " . $page . " - Line: " . __LINE__;
mysql_close();
sendem(error, .1);
die();
}

$selectdb = mysql_select_db("testy");
if(!$selectdb)
{
$_SESSION['error_message'] = mysql_error();
$_SESSION['error_location'] = "Page: " . $page . " - Line: " . __LINE__;
mysql_close();
sendem(error, .1);
die();
}

$sql = "SELECT `name`, `id` FROM `users`";
$query = mysql_query($sql);
if(!$query)
{
$_SESSION['error_message'] = mysql_error();
$_SESSION['error_location'] = "Page: " . $page . " - Line: " . __LINE__;
mysql_close();
sendem(error, .1);
die();
}

$num_rows = mysql_num_rows($query);
$get = mysql_fetch_assoc($query);
mysql_close();
echo "<table width='40%' align='left' border='1'>\r\n";
echo "<tr>\r\n";
echo "<td align='center'>##</td>\r\n";
echo "<td align='center'>Members</td>\r\n";
echo "<td align='center'>ID</td>\r\n";
echo "</tr>\r\n";
for($g = 1; $g <= $num_rows; $g++)
{
echo "<tr>\r\n";
echo "<td>" . $g . "</td>\r\n";
echo "<td>" . $get['name'] . "</td>\r\n";
echo "<td>" . $get['id'] . "</td>\r\n";
echo "</tr>\r\n";
}
echo "</table>\r\n";
?>

You need to call mysql_fetch_assoc() once for each row.  Each time you call it, it will return the next row.  Typcially it looks like this:

 

while ($row = mysql_fetch_assoc($result)) {
  // do stuff with $row
}

Alright thanks! I was confused because php.net said it moves the internal pointer ahead each time it is called. I didn't realize that they meant the function.

 

And I was also wondering, since you sorta brought it to my attention.

 

What is the difference between, mysql_fetch_array(), mysql_fetch_asssoc() and mysql_fetch_row()?

 

I understand that mysql_fetch_array() and mysql_fetch_row() both use arrays, but doesn't mysql_fetch_assoc() use an array also?

 

And I was also wondering how they function differently?

 

I've been confused about this since I first started doing anything with mysql...

 

Thanks!

The question is how you want to get your row back -- as an array of column values, or as a hash keyed by column name.  As you correctly pointed out, there are a variety of ways to ask for either, some more explicit than others.

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.