Skip to content
Advertisement

How to user fetch_assoc() twice in a single php file?

I have two table. One is an article list and the other one is a category list. In the category table, I have use fetch_assoc() to get the data from my DB and list in the table. But I want to fetch the article’s data in the article table. How do I use fetch_assoc() twice in the same php file?

JavaScript

articles table

JavaScript

Advertisement

Answer

Replace

JavaScript

with

JavaScript

It does the same but the foreach approach uses an automatic iterator that will always start from the beginning of the result set.

JavaScript

If for some reason, you must use while loop and the manual approach then you need to ensure that you always reset the internal pointer to the first records before starting your loop. However, manual looping is not recommended. It is easier to make more errors when doing this manually with while

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement