phpguy11
phpguy11

Reputation: 23

Reading values from mysqli result

I am trying to read an compare values from mysqli query but unsuccessfully.

This is example of my code:

$sql = "SELECT team, won, lost, points, goals_for, goals_agn FROM teaminfo WHERE tour_id=5 ORDER BY points DESC, (goals_for - goals_agn) DESC, goals_for DESC ;";
$query = $this->db_connection->query($sql);

so its simple sql query where I grab all teams from teaminfo DB and I want to compare them according to certain criteria (points, goal scored...) to see which team will go into the next phase of the competition.

Its is simple to print ALL values with something like loop:

while ($team=mysqli_fetch_array($query)) {... some code ...}

But I dont need this. I want to access only few of them and compare them. For example:

//Compare 3rd team points from $query with 4th team points from $query
     If ($team[3]['points']==$team[4]['points']) 
{
       I would do something..

    } else ....

I just want "transform" $query so I can use all data from it manually. I tried some stuff from php.net for fetching data but like I said, all was unsuccessfull.

Upvotes: 1

Views: 91

Answers (1)

DocRattie
DocRattie

Reputation: 1423

You could put all the data in an array and than work with that.

$teams = array();
while ($team=mysqli_fetch_array($query)) {
  $teams[$team['team']] = $team;
}

Then you can access them via $teams['teamname']

if($teams['teamA']['points'] == $teams['teamB']['points']){
    // do something
}

its possible to just use

 $teams[] = $team;

To fill the array. But I'd recommand an associative filling as that makes it easiert to access the data for a team. With only a numeric entry you'd have to check for their names if you want to use data of specific teams.

Upvotes: 2

Related Questions