-2

When I select all from my database, it only returns one row. I have changed it to select "column name" from database where "blank" = 1 and set multiple rows to have "blank" = 1 and it still only returns one row.

Here is my code:

<?php
    $link = mysqli_connect("host", "database", "password", "database");
    if (mysqli_connect_error()) {
        die("There was an error, Please try again");
    }
    $query = "SELECT * FROM `news`";
    $result = mysqli_query($link, $query); 
    $imageTest = mysqli_fetch_array($result);
?>

When I test this and print out the array with

<?php print_r($imageTest); ?>

It only comes back with one row (there are currently 3 test rows in the database)

I know Im probably missing something super small. Ive never really worked a project that I needed to select more than one row of items in a column.

I have another database being used in the same project that Im not having any issues with in its intended function but when I went to that one and tested the same thing, it returned the same results.

maxisme
  • 3,420
  • 4
  • 37
  • 82
Jacob Hilt
  • 23
  • 3
  • Possible duplicate of [Fetch rows using mysqli](https://stackoverflow.com/questions/20125791/fetch-rows-using-mysqli) – instead Mar 25 '18 at 01:09

2 Answers2

2

As per official documentation, mysqli_fetch_array fetch a single row from the result set every time it's called. Go for the following code to retrieve all of them:

$rows = array();
$result = mysqli_query($link, $query);

while ($row = mysqli_fetch_array($result))
    $rows[] = $row;

print_r($rows);
Tommaso Belluzzo
  • 21,428
  • 7
  • 63
  • 89
-1

mysqli_fetch_array fetches a single row from the result set.

What you're looking for is mysqli_fetch_all (or a while loop with mysqli_fetch_array ).

I would advise against using mysqli_* functions, and suggest using PDO.
Please refer to the following resources as to why I hold this opinion:

mysqli or PDO - what are the pros and cons?
https://phpdelusions.net/pdo/mysqli_comparison

ekstrakt
  • 820
  • 1
  • 12
  • 24