Jump to content

[SOLVED] Fetch multiple rows and echo


smc

Recommended Posts

Okay, I've been trying to do this for a while but I never succeed and end up doing a work around to solve my problem. I'm tired of that, time to stare PHP/MySQL in the eye and say FETCH THAT ARRAY!

Basically heres the deal.

I want to call everything in my database within an HTML page that meets a specific MySQL critera ( WHERE status = 'pending' ) . I want to then echo all of those in a table format. This means I would need to generate a new table (or row) [In HTML] for all of these entries MySQL found.

I tried this:

[code]
function listusers() {
//Query for execution
$fetchfido = mysql_query('SELECT * FROM `users` ORDER BY `users` . `username` ASC');
//While we are grabbing the array we will go ahead and prep the HTML for echoing
while($users = mysql_fetch_array($fetchfido)){
$listusers = "<a href=\"/admin/admin_users.php?user=$users[username]\">$users[username]</a><br />";

}
}
[/code]

But it only echoed the last row. Snafo.

Any help you can offer for me to battle this tourmenter would be much appreciated!!

Thanks!
Link to comment
https://forums.phpfreaks.com/topic/36127-solved-fetch-multiple-rows-and-echo/
Share on other sites

Few small changes, but it should work.. or it'll tell you the error. I think it only echo's 1 because there is only 1 $listusers.

[code]<?php
function listusers() {

$result = mysql_query("SELECT * FROM `users` ORDER BY `users`, `username` ASC") or die(mysql_error());

while($row = mysql_fetch_array( $result )) {

echo "<a href=\"/admin/admin_users.php?user=$users[username]\">".$row['username']."</a><br />";

}
}
?>[/code]
What you are doing is correct.

however that won't output anything, it just puts it in a variable...

but to create your table you'd just need.
[code=php:0]
<?php
$query = "YOUR QUERY";
$result = mysql_query($query);

echo "<table>\n";
while($row = mysql_fetch_assoc($result)) {
  echo "<tr><td>{$row['field_1']}</td><td>{$row['field_2']}</td></tr>\n";
}
echo "</table>\n";
?>
[/code]
i don't see what the problem is...
[code]
<?php
$sql = "SELECT * FROM users WHERE status = 'pending' ORDER BY username ASC";
$query = mysql_query($sql);

while($row = mysql_fetch_array($query)){
echo "<a href=\"/admin/admin_users.php?user=". $row['username'] ."\">". $row['username'] ."</a><br />\n";
}
?>
[/code]

you may need to add return; to the end of your function too. try that query and see if it works.

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.