nexus6
nexus6

Reputation: 23

php mysqli stmt

i'm new to the php mysql usage. i'm selecting the results from database .. and i'm cycling/printing them out to the screen using while($stmt->fetch()): .. what i'd like to do is to cycle through the results again after the first cycle without calling the database (frombuffered results set).

i'm using php5, mysqli, stms on xampp server.

Upvotes: 1

Views: 334

Answers (2)

fredley
fredley

Reputation: 33941

while($row = $stmt->fetch()){
  $storedRows[] = $row;
  //do stuff
}
foreach($storedRows as $row){
  //do more stuff
}

Upvotes: 1

codaddict
codaddict

Reputation: 455460

You can use arrays.

When you cycle through the result for the first time you can put the values in the array and later in the 2nd cycle you can access the elements from the array.

Something like:

$query = "SELECT name FROM EMP";

$arr = array();
if ($stmt = $mysqli->prepare($query)) {

        $stmt->execute();                                  

        $stmt->bind_result($name);

        // 1st cycle.
        while ($stmt->fetch()) {
                $arr[] = $name; // save in array.
        }   

        $stmt->close();

        // 2nd cycle.
        foreach($arr as $name) {
                // use $name again.
        }   
}

Upvotes: 0

Related Questions