0

I´m executing a mysqli statement in a php file, and I want to pass the results to another php page. I´m storing the result in a session variable, but when I tried to show them in the second php page it appears this error:

Warning: mysqli_result::fetch_array(): Couldn't fetch mysqli_result

First php file:

$stmt->execute();
$result = $stmt->get_result();
$_SESSION['result'] = $result;

Second php page:

<tbody>
    <?php
    while ($row = $_SESSION['result']->fetch_array(MYSQLI_NUM)) {
        foreach ($row as $r) {
            ?>
            <tr>
                <td><input type="radio" name="selected" id="selected" checked="checked"></td>
                <td> <?php
                    if (isset($_SESSION['num'])) {
                        echo $_SESSION['num'];
                    }
                    ?></td>
                <td>4</td>
                <td>430</td>
                <td>648</td>
                <td>1,055</td>
                <td>-60</td>
                <td>6,448</td>
                <td>7,521</td>
                <td>0,561</td>
            </tr>
        <?php
        }
    }
    ?>
</tbody>

I have tried to show the results in the first php page with the same form that I want to show them in the second and it works, but when I do it in the second page it doesn´t work

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98

1 Answers1

2

Your problem is that when the first PHP page finished executing, the output of get_result was invalidated as it is just a pointer to a mysqli::result object which no longer exists. To work around this you can either save all the results of the query e.g.

$_SESSION['results'] = $result->fetch_all();

and then in the second file

foreach ($_SESSION['results'] as $row)

or execute the query again in the second PHP file. You will need to trade off the storage required to save all the results against the time taken to re-execute the query.

Nick
  • 138,499
  • 22
  • 57
  • 95