Biske94 Posted December 31, 2017 Report Share Posted December 31, 2017 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 Thanks in advance! Quote Link to comment Share on other sites More sharing options...
administrator Posted January 1, 2018 Report Share Posted January 1, 2018 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 1 Quote Link to comment Share on other sites More sharing options...
Biske94 Posted January 2, 2018 Author Report Share Posted January 2, 2018 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. Quote Link to comment Share on other sites More sharing options...
administrator Posted January 2, 2018 Report Share Posted January 2, 2018 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 1 Quote Link to comment Share on other sites More sharing options...
Biske94 Posted January 2, 2018 Author Report Share Posted January 2, 2018 I get it now! Thanks Stef! :)) 1 Quote Link to comment Share on other sites More sharing options...
administrator Posted January 2, 2018 Report Share Posted January 2, 2018 Very uptodate avatar image! ... May the code be with you. S 1 Quote Link to comment Share on other sites More sharing options...
Biske94 Posted January 2, 2018 Author Report Share Posted January 2, 2018 *answers in porg* P.S. The code is strong in you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.