Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FOREACH - from mysql 1

Status
Not open for further replies.

Momozone

IS-IT--Management
Dec 2, 2004
103
IE
I have a query:

$servers_query = "SELECT * FROM servers";

which i grab the result in a variable:

$servers_result = mysql_query($servers_query);

then I put the result into an array:

$arrserver = mysql_fetch_assoc($servers_result);

and try to print using foreach:

foreach ($arrserver as $row)
{
echo $row;
}

However no matter what i do i keeping only the first row of data?

1Server1010 Ext System999.99

Despite the structure of the database being:

+----+-------------+---------------+--------+
| id | productname | description | price |
+----+-------------+---------------+--------+
| 1 | Server10 | 10 Ext System | 999.99 |
| 2 | Server20 | 10 Ext System | 999.99 |
| 3 | Server20 | 10 Ext System | 999.99 |
+----+-------------+---------------+--------+

Aslo if i do

echo $row['productname'];

I seem to alway only get the first character of each feild of row 1 only.

Can anyone help - I am new to this!!

MoMoZoNe
 
Remove the line...
$arrserver = mysql_fetch_assoc($servers_result);

and replace the foreach with
while (mysql_fetch_assoc($servers_result)){

then use
echo $row['productname'] . "\n";
to echo each row.

Code:
$servers_query = "SELECT * FROM servers";
$servers_result = mysql_query($servers_query);

while (mysql_fetch_assoc($servers_result)){
echo $row['productname'] . "\n";
}

mysql_free_result($servers_result);
mysql_close();

Mark
 
but shouldnt foreach work?

MoMoZoNe
 
I tried your example above but am just getting a blank page?

Doesnt appear to return anything?




MoMoZoNe
 
actually using the while i only seem to be getting the following results:

9 9

which is odd!!!!

when i count the rows of data it tells me there is 3 - which is correct

mysql_numrows($servers_result)

this returns 3 rows.

Why is foreach or while not working?

MoMoZoNe
 
foreach is a construct that iterates arrays and objects. mysql_fetch_assoc will return an array but each array will be only one row's worth of data. so your foreach will simply be giving you the columns present in the first row.

Mark's advice represents the usual best practice. use a while loop. to get at the columns within each row, a foreach loop is a good start.
 
jpadie - I tried Marks suggestion and it didnt work.

I tried this way after a few googles and its works:

for ($i=0; $i<$numserver; $i++) {
foreach ($setarrserver as $key => $row)

{

echo ( $key=="productname" ) ? $row : "";

}
$row++;
}


MoMoZoNe
 
that's a bad way to do it.

while {} is the good way.

Mark's example was slightly wrong however. he should have typed

Code:
while ([red]$row = [/red]mysql_fetch_assoc($servers_result)){
 
ok so that works , sorta - The first row is now missing???

Its only printing the second row onwards?




MoMoZoNe
 
probably because you have an earlier call to mysql_fetch_assoc.

the mysql_fetch_* functions iterate the recordset. each call advances the cursor by one record.
 
Ah yes - many thanks for your help.




MoMoZoNe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top