我无法访问php关联数组中的项目

时间:2016-03-20 07:44:21

标签: php arrays string

当我使用print_r时,我无法在我的数组中回显这些内容这就是我所看到的

Array ( [Name] => blank [score] => 88 )<br/>
Array ( [Name] => Jeehbs [score] => 32 )<br/>
Array ( [Name] => Jeehbs [score] => 20 )<br/>
Array ( [Name] => blank [score] => 9 )<br/>
Array ( [Name] => blank [score] => 5 )

这就是我在使用var_dump

时看到的内容
array(2) { ["Name"]=> string(5) "blank" ["score"]=> string(2) "88" }<br/>
array(2) { ["Name"]=> string(6) "Jeehbs" ["score"]=> string(2) "32" }<br/>
array(2) { ["Name"]=> string(6) "Jeehbs" ["score"]=> string(2) "20" }<br/>
array(2) { ["Name"]=> string(5) "blank" ["score"]=> string(1) "9" }<br/>
array(2) { ["Name"]=> string(5) "blank" ["score"]=> string(1) "5" }

这是代码

while($row = mysql_fetch_array($comments, MYSQL_ASSOC))
  {
     $name = $rows['Name'];
     $score = $rows['score'];
     print_r($row); 

     echo '<div class="name">'.$name.'</div><div class="score">'.$score.'</div>';
  }  

3 个答案:

答案 0 :(得分:2)

我猜你写了一个错字,它是:

$name = $row['Name'];
$score = $row['Score'];

答案 1 :(得分:0)

您宣布$row,但您使用的是$rows。请按以下步骤操作:

while($row = mysql_fetch_array($comments, MYSQL_ASSOC))
  {
     $name = $row['Name'];
     $score = $row['score'];
     print_r($row); 

     echo '<div class="name">'.$name.'</div><div class="score">'.$score.'</div>';
  }  

答案 2 :(得分:0)

在while循环中,您应该使用$row而不是$rows

此外,如果您想整齐地打印数组结果,可以在echo "<pre>";之前添加print_r,如下所示:

while($row = mysql_fetch_array($comments, MYSQL_ASSOC))
  {
     $name = $row['Name'];
     $score = $row['score'];
     echo "<pre>";
     print_r($row); 

     echo '<div class="name">'.$name.'</div><div class="score">'.$score.'</div>';
  }