⊗ppPmDBLAR 368 of 447 menu

Forming an Array in a Loop with an SQL Query in PHP

Of course, manually iterating through all employees is not very convenient. Let a loop do it for us:

<?php for ($data = []; $row = mysqli_fetch_assoc($res); $data[] = $row); var_dump($data); // here will be an array with the result ?>

Let's figure out how this loop works.

In each iteration of the loop, the mysqli_fetch_assoc function sequentially reads each row of the result, writing it to the $data array.

As soon as the rows in $res run out, mysqli_fetch_assoc will return NULL and the loop will finish its work. The obtained result will be in a two-dimensional array $data.

Using the described loop, get and display an array of all employees via var_dump.

From the obtained result, get the first employee. Display their name via echo.

From the obtained result, get the second employee. Display their name and age via echo.

From the obtained result, get the third employee. Display their name, age, and salary via echo.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline