-2

I have a MySQL server and I want to grab X lines from a database with a PHP instruction. Here is my command and my database info (quote = "Q-1-Rev04"):

$quoteLineRequest = "SELECT `id`, `quantity`, `unitprice`, `goodsref`  FROM `quoteline` 
WHERE `quote` = '".$quoteid."'";
if($result = mysqli_query($link, $quoteLineRequest)){
 if (mysqli_num_rows($result) > 0){
  $quoteLines = mysqli_fetch_assoc($result);
  var_dump($quoteLines);
 }
}

database values

but when i'm var_dumping $quoteLines, it only give me 4 results instead of the 12 expected:

array(4) { ["id"]=> string(3) "113" ["quantity"]=> string(4) "1100" ["unitprice"]=> string(4) "0.26" ["goodsref"]=> string(8) "100-0011" }

Why all corresponding values are not grabbed?

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 2
    (Possible) side note: Do not use string interpolation or concatenation to get values into SQL queries. That's error prone and might make your program vulnerable to SQL injection attacks. Use parameterized queries. See ["How to include a PHP variable inside a MySQL statement"](https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-statement) and ["How can I prevent SQL injection in PHP?"](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – sticky bit Oct 27 '21 at 09:49
  • 1
    Did you read the documentation page for mysqli_fetch_assoc? https://www.php.net/manual/en/mysqli-result.fetch-assoc.php . It tells that that this function returns one row at a time from the result. It also contains examples of how to use a loop to fetch all the rows - there are also examples of that in a billion other places online. Almost any PHP/SQL tutorial would be likely to show you how it's done. – ADyson Oct 27 '21 at 09:49

1 Answers1

0

This is because mysqli_fetch_assoc only fetches one row from your query results.

If you want to fetch all the rows, you'll need to use fetch_all:

if (mysqli_num_rows($result) > 0) {
    $quoteLines = fetch_all($result, MYSQLI_ASSOC);
    var_dump($quoteLines);
}
Benoit Esnard
  • 2,017
  • 2
  • 24
  • 32