While ($Row = MySQL_Fetch_Array($Result)) - How Many Loops Are Being Performed

while ($row = mysql_fetch_array($result)) - how many loops are being performed?

I'm assuming mysql_fetch_array() perfroms a loop, so I'm interested in if using a while() in conjunction with it, if it saves a nested loop.

No. mysql_fetch_array just returns the next row of the result and advances the internal pointer. It doesn't loop. (Internally it may or may not use some loop somewhere, but that's irrelevant.)

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

This does the following:

  1. mysql_fetch_array retrieves and returns the next row
  2. the row is assigned to $row
  3. the expression is evaluated and if it evaluates to true, the contents of the loop are executed
  4. the procedure begins anew
$row = mysql_fetch_array($result);
foreach($row as $r) {
...
}

This does the following:

  1. mysql_fetch_array retrieves and returns the next row
  2. the row is assigned to $row
  3. foreach loops over the contents of the array and executes the contents of the loop as many times as there are items in the array

In both cases mysql_fetch_array does exactly the same thing. You have only as many loops as you write. Both constructs do not do the same thing though. The second will only act on one row of the result, while the first will loop over all rows.

use a while mysql_fetch_array and UPDATE during the loop

Update the specific (current) row in the while loop

Currently it is doing like this when while loop reach at the end of iteration it has the total 21 and it updates table with value 21 hence all the rows contains 21.

Your update Query is updating all rows every time
put here condition for the unique id to update

$sql=mysql_query("UPDATE table SET total_val = '$values' WHERE id=$row['id']");

PHP statement: while($row = mysql_fetch_array($query))

The variable $query is a resource and as such, has some sort of internal "row counter". Every time you call mysql_fetch_array() or a variant thereof, this counter gets incremented. As a result, every time you call this function mysql_fetch_array, you get the next row. When the last row has been received, and you call it again, it returns false. Because of the way a while loop works, you break out of this loop.

putting mysql_fetch_array with the same result set within two loops doesn't work

after I answered I've done some more research and posted the correct version in the edit below

Because it's the way array iterators work in PHP, resetting the pointer should do the trick:

while($row=mysql_fetch_array($result))
{
//some code
}
// reset($result); - won't work with mysql_query() result
mysql_data_seek($result, 0);
while($row=mysql_fetch_array($result))
{
//some code
}

Read more on the reset() function here

EDIT: after more research I found I was wrong - use mysql_data_seek:

mysql_data_seek($result, 0);

php/mysql - while loop ..pass result in array?

You can either use the json_encode for this to get the proper json output
or
you can get rid of this "," (comma) by using this technique.

//first use an variable $cnt=0;
//then check into the while loop
//whether the $cnt==0 then not to put the , before the making of an entry
//for example,
<?php
include('../connectDB.php'); //connect to db
$cnt=0;

echo '{ ';
echo '"success": 1, ';
echo '"result": [ ';
$result = mysql_query("SELECT * FROM roominventory");
while($row = mysql_fetch_array($result)) { //start while
$getId = $row['id']; //get value
$getRoomId = $row['room'];
if($cnt==0)
{
echo '{ ';
}
else
{
echo ',{';
}
$ar = $row['arrival']; //assign value to variable
$dep = $row['departure'];

$date = str_replace('-', '/', $ar); //format the date
$formatArr = date('m/d/Y', strtotime($date));

$date2 = str_replace('-', '/', $dep); //format the date
$formatDep = date('m/d/Y', strtotime($date2));

$mSt = strtotime($formatArr) * 1000; //convert to milliseconds
$mEd = strtotime($formatDep) * 1000;

echo '"id": ' . '"' . $getId. '"' . ', ';

$resulta = mysql_query("SELECT * FROM rooms_amenities WHERE id='$getRoomId'");
while($rowa = mysql_fetch_array($resulta)) {
$getName = $rowa['name'];
}

echo '"title": ' . '"' . $getName . '"' . ', ';
echo '"url": ' . '"' . 'http://example.com' . '"' . ', ';
echo '"class": ' . '"' . 'event-warning' . '"' . ', ';
echo '"start": ' . '"' . $mSt . '"' . ', '; //echo the converted date
echo '"end": ' . '"' . $mEd . '" ';
echo '} ';
//echo ', '; //comma (should only in between entries and not in the very end of the very last entry)
$cnt=1;
} //end while
echo '] ';
echo '}';

PHP while loop. Why does this work?

You can rewrite that code to this equivalent:

$row = mysql_fetch_array($result); // fetch and advance
while ($row !== false) { // compare result against false
echo $row[1]. " ".row[2]."<br/>";
$row = mysql_fetch_array($result); // fetch and advance
}

An assignment yields a value which can then be used in a condition, though it's a good practice to put parentheses around the assignment:

// fetch and advance, compare fetched result against false
while (($row = mysql_fetch_array($result)) !== false) {
echo $row[1]. " ".row[2]."<br/>";
}


Related Topics



Leave a reply



Submit