Maksim Ivanov
Maksim Ivanov

Reputation: 4331

mysqli - How to fetch a row without modifying the result set?

Is it possible to extract a value from mysqli_result without fetching a row i.e. without modifying the result set as I need it in full later in my code?

Upvotes: 2

Views: 342

Answers (2)

Bryan
Bryan

Reputation: 3494

You can also point the iterator back to the beginning. ie..

$result = $mysqli->query($query);
while($row = $result->fetch_assoc())
{
    //do stuff with the result
}
//back to the start
mysqli_data_seek($result,0);

now the pointer is returned to the beginning. mysqli_data_seek

Upvotes: 2

bjb568
bjb568

Reputation: 11498

You can get all and store in an array…

$results = [];
$sql = mysqli_query($con,"SELECT * FROM WHATEVER");
while ($row = mysqli_fetch_array($sql)) {
    array_push($results, $row);
}
//Now $results is fully populated

Upvotes: 0

Related Questions