简体   繁体   中英

error printing array when looping over it

Sorry for the relatively newbie question. Ive been stuck the past hour trying to figure out why the array named $propname does not want to print when i loop over it. Here follows my code

$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 ++;
}

Just simplify your code

$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'];
}
$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'];
}

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