Forum Moderators: coopster

Message Too Old, No Replies

mysql_fetch_row()

Loop isn't looping...

         

createErrorMsg

4:50 am on Nov 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I believe you can use a SELECT statement, mysql_fetch_row() and a foreach loop to cycle through all the rows in a table and extract information, but I have not been able to make this happen. My understanding of foreach is admittedly murky. Here's what I have so far...

$query = "SELECT * FROM table WHERE id>0 ORDER BY id";
$result = mysql_query($query) or die(mysql_error());
$fetched = mysql_fetch_row($result);
foreach ($fetched as $row) {
echo($row); //THIS IS ONLY ECHOING ONE ROW INSTEAD OF LOOPING. FOR INSTANCE, WITH THREE ROWS IN THE TABLE, THIS ECHOS ONLY THE FIRST ONE AND STOPS
}

Ultimately, I want to loop through all the rows in the table, then dip into the contents of each row to extract a single value for use in constructing links, but I'd be happy for the moment being able to just echo the full contents of each row. Accomplishing this will almost certainly lead me toward my ultimate goal.

Could anyone shed some light here on getting all the rows echoed? Am I using my SELECT syntax wrong, or is it the foreach() loop that isn't right?

I feel like I've been asking a lot of really newbie questions over the last few days, so thanks for your patience and understanding.

cEM

grandpa

5:15 am on Nov 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$query = "SELECT * FROM table WHERE id>0 ORDER BY id";
$result = mysql_query($query) or die(mysql_error());

this works for me
while ($fetched = mysql_fetch_row($result)) {
foreach ($fetched as $row) {
echo $row;
}
}

dreamcatcher

10:57 am on Nov 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why not use mysql_fetch_array or mysql_fetch_assoc?

$query = "SELECT * FROM table WHERE id>0 ORDER BY id";
$result = mysql_query($query) or die(mysql_error());

while ($fetched = mysql_fetch_assoc($result))
{
echo $fetched['rowname'];
}

dc :)

Bonusbana

11:06 am on Nov 24, 2004 (gmt 0)

10+ Year Member



I always use objects:

$result = mysql_query("SELECT * FROM table WHERE id>0 ORDER BY id") or die(mysql_error());
while ($row = mysql_fetch_object($result)) echo $row->rowname;

createErrorMsg

3:04 am on Nov 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for all the responses. I wound up using mysql_fetch_assoc() as suggested by Dreamcatcher, simply because I'm familiar with working with associative arrays. All of your suggestions were good one's though.

Bonus, I'm not sure I'm ready to get into using php objects. I'm still trying to get the basics down and feel the object side might be over my head at the moment (to use an area where I have some measure of experience, it'd be like trying to design a complicated css-p layout while you're still learning how to style fonts and backgrounds). Am I right, or are PHP objects easy enough to learn right off the bat?

Thanks again for your help!

cEM

Robber

9:06 am on Nov 25, 2004 (gmt 0)

10+ Year Member



Regardig objects, dont panic! The mysql_fetch_object() is dead simple, you dont need to start learning OOP to use it.

You'd just do something like:

while($row = mysql_fetch_object($fetched)){
print $row->fieldname
}