循环时错误打印数组

时间:2013-09-28 02:00:42

标签: php mysql

抱歉这个相对较新的问题。 过去一小时我一直试图弄清楚为什么名为$ propname的数组在我循环时不想打印。这是我的代码

$result = mysql_query("SELECT * FROM `player_info` WHERE `position` = 'THP'") or die(mysql_error()) ;

while($row = mysql_fetch_array($result)) //create arrays
{   
    $id[] = $row['player_id'];      
    $propName[] = $row['name'];
    $propLastName[]= $row['surname'];
}//end while

 //create variables for looping
$x = sizeof($propName);
$index = 0;

while($index < $x) //print variables
{
    echo $propName[$index];
    $index ++;
}

2 个答案:

答案 0 :(得分:0)

简化您的代码

$result = mysql_query("SELECT * FROM `player_info` WHERE `position` = 'THP'") or die(mysql_error()) ;

while($row = mysql_fetch_array($result)) //create arrays
{   
    echo $row['name'];
}

答案 1 :(得分:0)

$result = mysql_query("SELECT * FROM `player_info` WHERE `position` = 'THP'") or             die(mysql_error()) ;

while($row = mysql_fetch_assoc($result)) //create arrays
{   
    $id = $row['player_id'];      
    echo $propName = $row['name'];
    $propLastName = $row['surname'];
}