tags:

views:

30

answers:

2
+1  Q: 

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.

A: 

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.
        }   
}
codaddict
thnx man! i'll store this lines straight into my code library ;-)
nexus6
what would be the best way to use more than just one field here in this exeample of yours? let say we have $street, $name and $phone
nexus6
You can use a multidimensional array. `$arr[] = array($name,$street,$phone);`
codaddict
i got it ... i have to set an array to an $arr[] instead of just $name
nexus6
+1  A: 
while($row = $stmt->fetch()){
  $storedRows[] = $row;
  //do stuff
}
foreach($storedRows as $row){
  //do more stuff
}
fredley
goddamn, this stackoverflow works!! damn! :-) thank for the answers
nexus6