0

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 )
4
  • What does the query look like? fetch_row not fetch_rows Commented Dec 2, 2014 at 18:50
  • Hi, the query is: $query ="SELECT * FROM WKaverage WHERE Average > 27.8"; but that works fine on mySQL. Commented Dec 2, 2014 at 18:51
  • 1
    You will need to call mysqli_fetch_row() for each single row. Commented Dec 2, 2014 at 18:51
  • wheres the rest of your query? Commented Dec 2, 2014 at 18:51

4 Answers 4

2

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
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks man, you are right! Will give you 'accept answer' in 8!
is $rows init required? (less code better code, easy to read code better code)
@Kyslik - no that won't be needed.
@Kyslik - Although I would strongly recommend that you initialize the array as it makes the code clearer and also shows what data $rows is meant to hold. Also, I don't agree that less code always means easier to read and better code
@KristerAndersson I asked that question in educational way, I know that in PHP you do not have to initialise variables. Some prefer less code some prefer more readable code. Thanks tho ;), MisterAnderson! good nick.
1

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

2 Comments

yes and no. ALL of the fetch functions return a single row. You need to fetch in a loop.
This will also just return a single row.
0

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

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.