How to use while to catch all lines

Asked

Viewed 21 times

-2

Hello, I want to take all the lines of the DB with the while instead of putting one by one and they turn variable according to the line of the db

<?php    
    $link = mysqli_connect($dba['host'], $dba['user'], $dba['pass'], $dba['db']) or die("Error " . mysqli_error($link));
    $sql = "SELECT * FROM clientes";
    $query = mysqli_query($link, $sql) or die(mysqli_error($link));
    
    while ($row = $query->fetch_assoc()) {
        $configuracao['perfil'] = $row['perfil'];
    }

I was wearing it inside the while:

$configuracao['perfil'] = $row['perfil'];    
$configuracao['idade']  = $row['idade'];    
$configuracao['local']  = $row['local'];

Has some form of the while transform variables according to what is in the database?

  • 1

    "transform the variables", Can you explain that part better? What kind of "transformation" do you imagine?

  • i was putting all db data into variables for example $setting['profile'] = $Row['profile']; $setting['age'] = $Row['age']; $setting['local'] = $Row['local']; manually, I want to know if there’s a while to do it for me instead of me for one by one

1 answer

0


From what I understand you want to press $configuracao the table data. Therefore, I believe that it would not be possible to dynamize the column identifier (which will be the position vector). You would need to describe all fields in the table for the association to be made:

while ($row = $query->fetch_assoc()) {
    $configuracao['perfil'] = $row['perfil'];    
    $configuracao['idade']  = $row['idade'];    
    $configuracao['local']  = $row['local'];
}

Note that the way it is, if you have more than one record in the table clientes, $configuracao will only have data of the last occurrence. If you want to store all, suggest the following:

while ($row = $query->fetch_assoc()) {
    $perfil = $row['perfil'];

    $configuracao['perfil'] = $perfil;    
    $configuracao[$perfil]['idade']  = $row['idade'];    
    $configuracao[$perfil]['local']  = $row['local'];
}

Thus, $configuracao would be an array of "profiles", and each profile would have an array with the other database data.

  • 1

    thank you very much, now I understand :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.