Home > Software engineering >  sql select in a php file and write out in for cycle
sql select in a php file and write out in for cycle

Time:11-14

I have a select in my php file, but the select result is have more rows, and i want to write out all rows with a for cycle.

I want to convert it after a qr code but i need only the writing out in the for cycle

//select command

$sql = "SELECT * FROM qr  
INNER JOIN eladott
ON qr.qr_eladott_id=eladott.eladott_id
WHERE qr.qr_eladott_id=$eladott_id";

    $result = mysqli_query($conn, $sql);
    
    if(mysqli_num_rows($result) > 0)
        
        $qr = mysqli_fetch_assoc($result);


 for ($i = 0; $i < $eladott_jegyek_db; $i  )
    {
        
        $pdf->Image($qr['qr_code']);
        
        
    }
    

CodePudding user response:

I've not used mysqli for some time, but this solution might suit you. Considering your database would be 'your_db' and the table 'user' exists within, consider the following code:

$link = mysqli_connect('localhost', 'root', '', 'your_db');

$result = mysqli_query($link, 'SELECT * FROM user');

while ($row = $result->fetch_assoc()) {
    echo $row['id'] . ' ';
}

My output would be:

28 29 32 35 45 46 48 53 55 56 57 59 61 62 63 65 66 67 70 71 74

The 'while' block will get all query selected rows as associative arrays under the $row variable.

  • Related