Use foreach loop instead of while with myslqli_fetch_array ()

Ok, so I understand that when:

//connection code;

//query code;

//$result= mysqli_query();

$row= mysqli_fetch_array($result);

      

you are creating an associative array where the column names from your table are the keys for the data in the corresponding row.

Then you can use:

while ($row= mysqli_fetch_array($result))
{
//code to echo out table data.
}

      

My question is, how does the while loop go to the next line after each iteration? I thought it needed foreach loops for this?

+3


source to share


7 replies


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 corresponding to the selected row and moves the internal data pointer forward.

+4


source


Many function returning a result set do this by returning an array in which you can do foreach()

as you are used to. However, this is not always the case, especially with database functions. mysqli_fetch_array

selects only one row, or returns boolean false

if there are no more. This is how the loop works: the expression is evaluated as true

long as there is a row to process.

The reason for this design is mainly efficiency. Retrieving database rows can be critical, and there are times when not all rows are needed. In these situations, this approach will give you a lot of flexibility. Collecting strings one by one is also more memory friendly since not all of the result data needs to be loaded into memory at once.



Actually Mysqli have a function that removes the entire array of results in the array: mysqli_fetch_all

. You can foreach()

do it for it.

+1


source


mysql_fetch_array simply fetches the next row of the result set from your mysql query and returns the row as an array, or false if there are no more rows to fetch.

The while loops continuously fetch results one at a time from the result set and continue until mysql_fetch_array becomes false.

The foreach loop loops through each value in the array. Since mysql_fetch_array is only fetching one result, so count ($ row) will be 1 every time.

+1


source


This part selects one row at a time

$row = mysqli_fetch_array($result);

      

By putting it in a while loop, it fetches one line at a time until it fetches a line, because it doesn't need to fetch any more.

An alternative would be to fetch all rows and then loop through them with foreach

$rows = mysql_fetch_all($result);
foreach($rows as $row){
    // do something with row
}

      

For this to work, you have to make yourself a function mysql_fetch_all

, which of course has the original while loop in it ...

function mysql_fetch_all($result)
{
  $all = array();
  while($thing = mysql_fetch_assoc($result)) {
    $all[] = $thing;
  }
  return $all;
}

      

0


source


This works because the SQL connector stores the current state of the query (i.e. the next result row to return) inside the result.

If you want a similar example, it works like reading from a file, where you can use similar constructs:

while ($line = fgets($fp, 1000)) {
    // ...
}

      

Behind the scenes (and depending on the language, interpreter, compiler, etc.) for

and while

essentially lead to the same code. The difference is that depending on what your code is supposed to do, one approach might be more readable than the other.

Take the following two loops as an example. Both do the same.

for ($i = 0; $i < 10; $i++) {
    // ...
}

$i = 0;
while ($i < 10) {
    // ...
    $i++;
}

      

0


source


Each time the while loop runs, it executes the mysql_fetch_array function and gets the next result. It does this until more results are shown.

0


source


mysql_fetch_array returns an array of rows corresponding to the selected row, or FALSE if there are no more rows. If the string exists, then get the data.

I hope this has been answered for you. It's hard to see what you mean

0


source







All Articles