0

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']);
        
        
    }
    
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Nov 13 '21 at 18:59

1 Answers1

0

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.

Moldovan Andrei
  • 305
  • 1
  • 6
  • 19
  • Its working, but how can i use it after? – Dániel Papp Nov 13 '21 at 17:29
  • row[$i] its not working in for cycle, maybe another option? – Dániel Papp Nov 13 '21 at 17:30
  • When you're using this method, the while WILL loop through all the rows that have been fetched from the database. It is possible to store them in another array to have access to them later on. Namely, $results_arr = []; whille ($row = $result->fetch_assoc()) { $result_arr[] = $row; } – Moldovan Andrei Nov 13 '21 at 17:32
  • 1
    thank you so much, its working. i didnt know how can i store anything in php. – Dániel Papp Nov 13 '21 at 17:39
  • You can use session and variables to store data in php. You might want to look at W3Schools articles on variables and arrays and on this link https://www.w3schools.com/php/php_sessions.asp to better your understanding c: – Moldovan Andrei Nov 13 '21 at 17:41