user6912198
user6912198

Reputation:

How does the mysqli_fetch_assoc function work?

I have one question about mysqli_fetch_assoc in a while loop.

$query = "SELECT * FROM category ";

$result = mysqli_query($connection, $query);

while($row = mysqli_fetch_assoc($result))
{
    $cat_id = $row['cat_id'];
    $cat_title = $row['cat_title'];

    echo $cat_id . " " . $cat_title  ."<br>";
}

So, how does $row = mysqli_fetch_assoc($result) work?

So it loops one row at a time from $results and stores that information in $row until it there is no row to return?

And this mysqli_fetch_assoc($result) in while loop iterations is this " array(some rows that it got from $result) "?

$row = mysqli_fetch_assoc($result) is same as $row = array(all rows from $result that are gathered by mysqli_fetch_assoc) ?

And that means $row is actually an array, and every time it loops the information is not overwriten by new instead it is added?

Upvotes: 4

Views: 13482

Answers (3)

Avishka Dambawinna
Avishka Dambawinna

Reputation: 1215

The simplest explanation is,

When you run mysqli_query() For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object which contains data from the database.


  • mysqli_fetch_assoc() - Fetch a result row(one row from the mysqli_result object) as an associative array at each time it called and moves the internal pointer to the next. $row is used to store that associative array containing data of single row getting from the mysqli_result object'

you can retrieve data belongs to that raw by using $row['column_name']

  • The while loop does the calling of mysqli_fetch_assoc() over and over. After one row is fetched, it goes to the next one and so on till it reaches the end of the object and returns NULL, which means false and breaks the while loop.

Upvotes: 1

Dharman
Dharman

Reputation: 33238

Returns an associative array that corresponds to the fetched row or NULL if there are no more rows. - from PHP manual on mysqli_fetch_assoc()

Whenever you call get_result() or mysqli_query() you will receive a mysqli_result object. This object contains the data fetched from the MySQL database server. The data from SQL is returned to PHP in rows. The purpose of mysqli_fetch_assoc() is to fetch a row from this object and move an internal pointer to the next row. Next time you call this method on the same object it will fetch a second row and move the pointer to the next row. Once a pointer moves beyond the last row this function will keep on returning NULL.

The values in each of the returned arrays correspond to a single row from the MySQL result set. The values are stored as an associative array. Each key is a column name from the SQL.

The purpose of the while loop is to simply call the same function over and over again until it returns NULL.

Bear in mind that using while loop is not the best approach. It is better to simply use foreach.

$query = "SELECT * FROM category ";

$result = $connection->query($query);
foreach ($result as $row) {
    $cat_id = $row['cat_id'];
    $cat_title = $row['cat_title'];

    echo $cat_id . " " . $cat_title  ."<br>";
}

Upvotes: 1

user6912198
user6912198

Reputation:

When using the mysqli_fetch_assoc() function, PHP is placing the data from the database into an associative array. You can then pull the data out of the array inside of the loop.

Since there is no incrementation to tell the loop when to stop, the while loop takes care of that. So you could read it like so:

While the mysqli_fetch_assoc() has more records, keep looping. When it runs out, stop.

Upvotes: 2

Related Questions