0

I'm trying to create an array of all the data in a table.

So far I am querying the database, and then iterating through the array to add each row to it.

But I'm getting an error I haven't seen before and cannot work out what it means:

Fatal error: An iterator cannot be used with foreach

Here is the code. I want to be grabbing the whole table, row by row, and appending it onto my $data array so I have an array of complete data from the table.

$result = mysqli_query($con, "SELECT * from everystory_Comp");
    foreach ($result as &$row){
        $row = mysqli_fetch_assoc($result);
        var_dump($row);
        array_push($data,$row);
    }
1
  • is & in &$row tipfeler? Commented Dec 23, 2014 at 12:33

5 Answers 5

0

You need to fetch your result. Use:

while ($row = mysqli_fetch_assoc($res)) {
  //...  
}
Sign up to request clarification or add additional context in comments.

Comments

0

try this:

while($row = mysqli_fetch_array($result))
{
var_dump($row);
}

Comments

0

Get all the entries at once via mysqli_fetch_all()

$query = "SELECT * from everystory_Comp";
$result = $con->query($query);     
$asArray = $result->fetch_all(MYSQLI_ASSOC);

Or if you are using a prepared statement:

$query = "SELECT * from everystory_Comp where countryCode = ?";

$stmt = $con->prepare($query); 
$stmt->bind_param("s", $countryCode);
$stmt->execute();

$resultSet = $stmt->get_result();
$asArray = $resultSet->fetch_all(MYSQLI_ASSOC);

Comments

-1

Try this :-

while ($row = mysqli_fetch_assoc($result)){
        var_dump($row);
        array_push($data,$row);
    }

Comments

-1

Try this, it should work.

<?php
    // code
    while($row=mysqli_fetch_assoc($result)) {
        $data[]=$row;
    }
    // code
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.