bobleny Posted February 23, 2007 Share Posted February 23, 2007 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"; ?> Link to comment https://forums.phpfreaks.com/topic/39741-solved-i-need-a-bit-of-help-with-this-members-list-script/ Share on other sites More sharing options...
btherl Posted February 23, 2007 Share Posted February 23, 2007 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 } Link to comment https://forums.phpfreaks.com/topic/39741-solved-i-need-a-bit-of-help-with-this-members-list-script/#findComment-191884 Share on other sites More sharing options...
worldworld Posted February 23, 2007 Share Posted February 23, 2007 Else you can also have : while ($row = mysql_fetch_array($result)) { // do stuff with $row } Link to comment https://forums.phpfreaks.com/topic/39741-solved-i-need-a-bit-of-help-with-this-members-list-script/#findComment-191905 Share on other sites More sharing options...
bobleny Posted February 23, 2007 Author Share Posted February 23, 2007 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! Link to comment https://forums.phpfreaks.com/topic/39741-solved-i-need-a-bit-of-help-with-this-members-list-script/#findComment-192079 Share on other sites More sharing options...
fenway Posted February 23, 2007 Share Posted February 23, 2007 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. Link to comment https://forums.phpfreaks.com/topic/39741-solved-i-need-a-bit-of-help-with-this-members-list-script/#findComment-192299 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.