简体   繁体   中英

Write positions of multidimensional array

i'm building a websystem for tournaments, and have problems with PDO objects and arrays, Basically I have a select with inner join taking data from the MySQL db using PDO, and I need to write every single position of my table, but dont know how. Already try uncontable ways, no sucess. This is my code:

$viewMatches = $conn->prepare("SELECT p.id, p.player_name FROM tbl_players p 
INNER JOIN tbl_matches m on p.id = m.player1_id
INNER JOIN tbl_tournaments t on $id = m.tournament_id = t.id");

$viewMatches->execute();
$rst = $viewMatches->fetchAll(PDO::FETCH_ASSOC);

So now I need to write the name of player locate in every part of the array, so I try this :

echo $viewMatches[0][1]; //no sucess

Return an error with PDO Statement

Need help, please.. I have found the way to write all the players, but to mount my championship table, I need to select manually the team in every position AND DONT KNOW HOW, please help! Thanks

Per the PHP Manual , PDO::FETCH_ASSOC: returns an array indexed by column name as returned in your result set.

You should be accessing the array by associative key, not by index:

<?php 
echo $rst[0]['player_name']; 
?>

<?php
foreach($rst as $row){
  foreach($row as $k => $v){
    echo "Key: $k : Value: $v"; 
  } 
} 
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM