Use MySQL_Fetch_Array() with Foreach() Instead of While()

Use mysql_fetch_array() with foreach() instead of while()

You can code like this:

$query_select = "SELECT * FROM shouts ORDER BY id DESC LIMIT 8;";
$result_select = mysql_query($query_select) or die(mysql_error());
$rows = array();
while($row = mysql_fetch_array($result_select))
$rows[] = $row;
foreach($rows as $row){
$ename = stripslashes($row['name']);
$eemail = stripcslashes($row['email']);
$epost = stripslashes($row['post']);
$eid = $row['id'];

$grav_url = "http://www.gravatar.com/avatar.php?gravatar_id=".md5(strtolower($eemail))."&size=70";

echo ('<img src = "' . $grav_url . '" alt="Gravatar">'.'<br/>');

echo $eid . '<br/>';

echo $ename . '<br/>';

echo $eemail . '<br/>';

echo $epost . '<br/><br/><br/><br/>';
}

As you can see, it's still need a loop while to get data from mysql_fetch_array

Use a foreach loop instead of while with myslqli_fetch_array()

From http://www.php.net/manual/en/function.mysql-fetch-array.php

array mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] )
Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.

Looping with while and foreach in PHP

First, just a point of style. You might consider renaming your variable $query to something like $results. It's holding the result of a query, not a query itself.

The problem is that you're not resetting $results. After the first table, you've iterated all the way through the array. When you get to the end, and there are no more rows to iterate over, mysql_fetch_assoc() returns false.

So try this:

foreach ($cals as $value)
{
while ($array = mysql_fetch_array($query))
{
if ($array['calendar'] == $value)
{
?>
<tr>
<td><?php echo $array['name']; ?></td>
<td><a onclick="update_form('<?php echo $array['name']; ?>', '<?php echo $array['calendar']; ?>')" href="#">Edit</a></td>
</tr>
<?php
}
}
echo "</table><br />Value: $value";
mysql_data_seek($query,0); // <=== Set the resultsets internal pointer back to zero (the first record).
}

The important bit is the mysql_data_seek() on the second to last line.

You could also stick that mysql_data_seek() right before the while() line, if you prefer. You just need to make sure that for each iteration of the foreach loop, the array pointer is reset before you hit while().

EDIT: s/reset/mysql_data_seek/

Mysql Fetch Array Foreach

foreach($result_list as $row) {

$productitems[] = array(
'id' => $row['id'],
'product' => $row['product'],
'price' => $row['price']
);
}

Try this.

Why while loop is always used to iterate through mysql_fecth_array() function result?

This is kind of dated information. In one way you're asking a question that is just accepted along the community. It is understood better that while I have information to show ... do something. You generally wouldn't say foreach of these items ... do something though you could. However, the other problem is mysql_fetch_array returns FALSE if there are no more rows. This would not work in a foreach because it is not an array. A for would also fail because to check for the finishing of a for you have to go to some point and end.. FALSE is not a valid point of check (that I have ever tried or used).


@Michael Berkowski Adds:

As of PHP 5.4+, the mysqli_result class does have Iterator support, meaning you can do $result = mysqli_query(...); and subsequently foreach ($result as $row) {...} and it will fetch associative arrays

Though this isn't to be said the most commonly used form which is why we have the question.


You could do...while but why would you. You don't have most the information that you're going to need from the fetch array.

While is accepted and generally better. Doesn't fail and has a fall back if the mysql fails any way.

Lastly... don't use mysql_* anymore. Switch to mysqli_*. Safer.. smarter.. better.

Loop mysql_fetch_assoc using foreach instead of a while loop

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.

Save yourself from trouble, don't use mysql_fetch_assoc and foreach together.

replace while loop with foreach

what you have tried looks good. there is no "fetch all" function in the old and deprecated mysql library. you should switch to mysqli or PDO instead.

in PDO you can just grab all the result-data with $statement->fetchAll() for example.

if you still want to solve your problem with the old mysql library, then:

$categories_query = tep_db_query("select categories_id, categories_name from categories order by categories_name");
while ($categories = mysql_fetch_array($categories_query))
{
$categories_array[] = array('id' => $categories['categories_id'], 'text' => $categories['categories_name']);
}

// do something with your $categories_array here

foreach($categories_array AS $array => $row)
{
// you can output / access each row here e.g. with: $row['id']
// or you can do a second foreach-loop for the columns:
foreach($row AS $col => $data)
{
echo $data;
}
}


Related Topics



Leave a reply



Submit