Jeak
Jeak

Reputation: 53

Sql array only showing first result

I want to put the results in variables seperately but for that I will need to be able to call them separate. Right now I have this query that retrieves 6 results (tested with mysqli_num_rows). But when I print it, it will only shows the first row from my 6 results.

$result =  mysqli_query ($con, $query) or die("FOUT: " . mysqli_error($con));

echo "<p>mysqli_num_rows($result)</p>";

$row = mysqli_fetch_row($result);
print_r($row);
mysqli_free_result($result);

Results from

print_r($row) = 

Array ( [0] => Iran [1] => 28 )

Upvotes: 0

Views: 924

Answers (4)

Roh&#236;t J&#237;ndal
Roh&#236;t J&#237;ndal

Reputation: 27242

Try this it will work :

$result =  mysqli_query ($con, $query) or die("FOUT: " . mysqli_error($con));

echo "<p>mysqli_num_rows($result)</p>";

$rows = array();
$row = mysqli_fetch_row($result);

do
{
   $rows[] = $row;

}while($row = mysqli_fetch_row($result))

mysqli_free_result($result);

Upvotes: 0

Shir Gans
Shir Gans

Reputation: 2027

You should use while

    while ($row = mysqli_fetch_row($result)){
print_r($row);
//do more stuff...
}

You can use fetch_assoc instead of fetch_row to have logical array keys

Upvotes: 0

Cyclonecode
Cyclonecode

Reputation: 30131

To get all rows you will need to do something like:

$rows = array();
while($row = mysqli_fetch_row($result)) {
   $rows[] = $row;
}
// $rows will now contain all rows in the result set

Upvotes: 2

versalle88
versalle88

Reputation: 1137

Your function, mysqli_fetch_row(), only returns a single row result:

http://php.net/manual/en/mysqli-result.fetch-row.php

Try looping through like this:

while ($row = mysqli_fetch_row($result) {
    // Do something
}

Thanks,

Andrew

Upvotes: 1

Related Questions