Jump to content
Killersites Community
Biske94

mysqli_fetch_assoc in while loop

Recommended Posts

Hi people, 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 = mysql_fetch_assoc($result) works?

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? 

Sorry if i confused you with such questions :D

Thanks in advance! :)

 

 

Share this post


Link to post
Share on other sites

When using the  mysql_fetch_assoc function, PHP is literally placing the data from the database into an associative array. Then you loops, you can pull the data out of the array. 

Makes sense?

Stefan

  • Like 1

Share this post


Link to post
Share on other sites

Oooh so it put all data in assoc array, lets say it put 5 rows of data, and the while loop loops 5 times then? 

I am asking because there is no incrementation, how does it know where to stop?

Thanks Stef. :)

Share this post


Link to post
Share on other sites

The while loop takes care of that.  So you could read it like so:

While the 'mysqli_fetch_assoc' has records, keep looping. When it runs out, stop.

;)

Stef

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×