I'm trying to display results from my database, I didn't know how until I saw this way of doing it while($row=mysql_fetch_array($result) { //Code }
and then I saw that using this way is somewhat obsolete, so I would have to choose to use while($row = $query->fetch(PDO::FETCH_ASSOC)) { //Code }
. My problem is that I don't understand how the while works: At what point does it stop being true? What is the data that it iterates? In a nutshell, how does this code work?
At what point does it stop being true? The while is a loop that as long as there are rows in the query will remain true once the query runs out of data the loop ends... it's very simple.
What is the data that it iterates? It depends on what you request in your query; the index can be literal or numeric. and will read them by lines.
How does this code work?
The thing is:
That it would be the same
while($row=mysql_fetch_assoc($result))
in a procedural wayWhat the does
While
, is that it executes as long as the condition is true.Greetings.
Well, basically what this line does:
It is to return the data of each record that the query you made previously obtained.
The While loop stops once the fetch stops sending data.
You can access the data in the following way
Pd replace "fieldname" with the names of the fields you want to manipulate in your table.
In my opinion and criteria, the easiest way to query a table in the database is as follows:
From the point of view of security and also from the point of view of using less code and being as efficient as possible. I hope that the code above is helpful, if you have any questions leave me a comment and I will gladly answer.