简体   繁体   English

写入多维数组的位置

[英]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. 我正在为比赛建立一个网络系统,并且PDO对象和数组有问题,基本上我可以选择使用内部联接,使用PDO从MySQL数据库获取数据,我需要写表的每个位置,但是我不知道怎么样。 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 使用PDO语句返回错误

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. 根据PHP手册 ,PDO :: FETCH_ASSOC:返回一个在结果集中返回的,按列名索引的数组。

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"; 
  } 
} 
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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