PHP fetch into multidimensional array -


i trying use bind_result , while-fetch fetch set of data 2d array, have searched through internet solution, doesn't work expected. not sure goes wrong in code after few hours debugging. appreciate if can helps detect blind spot.

scenario:

i have table below:

tbl_team teamname    id      name    gender team1       1       john    male team1       2       kelly   female team1       3       chan    male team2       1       jordan  male ... etc 

i created function query these base on teamname.

<?php  function queryteam ($team) {   $stmt = $mysqli->prepare("select id, name, gender tbl_team teamname = ?");    if($stmt)   {      $stmt->bind_param('s',$team);      $stmt->execute();      $stmt->store_result();      $stmt->bind_result($team_member['id'],$team_member['name'],$team_member['gender']);      $team = array();      while($stmt->fetch())      {        //echo implode(" - ", $team_member)."<br>"; //debug purpose        $team[] = $team_member;      }      $stmt->close();      return $team;   } } ?> 

then have part of code call function , print result on screen:

<?php   $team_array = queryteam( "team1");   for($i = 0, $count=count($team_array);$i<$count;$i++)   {      echo "team1 members below:<br>";      echo implode(" - ",$team_array[$i])."<br>";   } ?> 

expected result like:

team1 members below: 1 - john - male 2 - kelly - female 3 - chan - male 

but in fact getting result below:

team1 members below: 3 - chan - male 3 - chan - male 3 - chan - male 

the query returns result correct tried echo during while(fetch), problem is, inside while, array overridden latest fetch result.

thanks in advance helps.

**edit have edited script , tested commented geo:

<?php    function queryteam ($team)   {     ....        $team = array();        while($stmt->fetch())        {          //echo implode(" - ", $team_member)."<br>"; //debug purpose          $team[] = $team_member;          echo "within fetch: <br>";          print_r($team); echo "<br>";        }        $stmt->close();        return $team;     }   }   $team_array = queryteam( "team1");   echo "function returned:<br>";   print_r($team_array); echo "<br>";   ?> 

i got result below:

within fetch: array([0]=>array([id]=>1 [name]=>john [gender]=>male)) array([0]=>array([id]=>2 [name]=>kelly [gender]=>female) [1]=>array([id]=>2 [name]=>kelly [gender]=>female)) array([0]=>array([id]=>3 [name]=>chan [gender]=>male) [1]=>array([id]=>3 [name]=>chan [gender]=>male) [2]=>array([id]=>3 [name]=>chan [gender]=>male)) function returned: array([0]=>array([id]=>3 [name]=>chan [gender]=>male) [1]=>array([id]=>3 [name]=>chan [gender]=>male) [2]=>array([id]=>3 [name]=>chan [gender]=>male)) array([0]=>array([id]=>3 [name]=>chan [gender]=>male) [1]=>array([id]=>3 [name]=>chan [gender]=>male) [2]=>array([id]=>3 [name]=>chan [gender]=>male)) array([0]=>array([id]=>3 [name]=>chan [gender]=>male) [1]=>array([id]=>3 [name]=>chan [gender]=>male) [2]=>array([id]=>3 [name]=>chan [gender]=>male)) 

thanks orangepill , evans_murithi!

i never thought caused bind_result

modified below:

<?php  function queryteam ($team) {   $stmt = $mysqli->prepare("select id, name, gender tbl_team teamname = ?");    if($stmt)   {      $stmt->bind_param('s',$team);      $stmt->execute();      $stmt->store_result();       //change part      //$stmt->bind_result($team_member['id'],$team_member['name'],$team_member['gender']);      $stmt->bind_result($id, $name, $gender);      /*******/       $team = array();      while($stmt->fetch())      {        $team_member = array();        $team_member['id'] = $id;        $team_member['name'] = $name;        $team_member['gender'] = $gender;        //echo implode(" - ", $team_member)."<br>"; //debug purpose        $team[] = $team_member;      }      $stmt->close();      return $team;   } } ?> 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -