PHP mysql_fetch_array

1

I'm trying to show results from my database, I did not know how until I saw this way of doing while($row=mysql_fetch_array($result) { //Code } and then I saw that using this form is somewhat obsolete, so I would have to choose to use while($row = $query->fetch(PDO::FETCH_ASSOC)) { //Code } . My problem is, I do not understand how the while works: At what moment does it stop being true? What are the data that iterates? In a few words How does this code work?

    
asked by Ricardo A 23.06.2017 в 23:13
source

4 answers

1

At what point does it stop being true? The while is a loop that while Existing rows in the query will remain true once the query runs out of data the loop ends ... it's very simple.

What are the data you iterate? 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?

while($row = $query->fetch(PDO::FETCH_ASSOC))
{#Mientrastenga registros devuelve true
    #lectura para la fila actual
    echo $row['indice Litera de columna'];
    echo '<br>'; #Salto de linea
}
    
answered by 23.06.2017 / 23:20
source
1

Well, basically what this line does:

while($row = $query->fetch(PDO::FETCH_ASSOC)) { //Code }

It is to return the data of each record that the query that you did previously obtained.

The While cycle stops once the fetch stops sending data.

You can access the data in the following way

while($row = $query->fetch(PDO::FETCH_ASSOC)) {
//usa este si estas obteniendo los datos en forma de objeto 
echo $row->nombredelcampo;
//usa este si estas obteniendo los datos en una matriz
echo $row["nombredelcampo"];
 }

P.d. replace "fieldname" with the names of the fields you want to manipulate from your table.

    
answered by 23.06.2017 в 23:32
1

What happens is:

- while($row=mysql_fetch_array($result) {  //Code } =  forma procedural
- while($row = $query->fetch(PDO::FETCH_ASSOC)) { //Code } esto es POO

That it would be the same has while($row=mysql_fetch_assoc($result)) procedurally

What the While does, is that it executes as long as the condition is true.

Greetings.

    
answered by 23.06.2017 в 23:32
1

In my opinion and criteria, the easiest way to consult a table in the database is as follows:

$connection = new PDO("mysql:host=localhost; dbname=database", "admin", "12345");
$connection -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$connection -> exec("SET CHARACTER SET utf8");

$age = 18;

$stmt = $connection -> prepare("SELECT * FROM USER WHERE AGE = :age");
$stmt -> bindParam(':age', $age);
$stmt -> execute();
$rows = $stmt -> fetchAll(PDO::FETCH_OBJ);
$stmt -> closeCursor();

foreach($rows as $user)
{
    echo $user -> NAME;
}

$connection = null;

From the point of view of security and also from the point of view of using less code and be as efficient as possible. I hope the above code is helpful, if you have any questions, leave me a comment and I will gladly answer.

    
answered by 24.06.2017 в 20:45