PHP无法回显数据

时间:2011-08-13 04:39:20

标签: php

我正在尝试从数据库中获取数据,并使用其唯一ID在页面上回显它们。下面是我的代码

<?php

session_start();

require_once('config.php');

    //create query statement
    $query = 'SELECT * FROM player_info';

    //make the query from db
    $myData = mysql_query($query, $conn)
    OR exit('Unable to select data from table');


    $row = mysql_fetch_array($myData, MYSQL_ASSOC);

    if(isset($myData)) {

        $row = mysql_fetch_array($myData, MYSQL_ASSOC);

        $playerID               = $row['id'];
        $player_bio             = $row['player_bio'];
        $achievements           = $row['player_achts'];
}


?>

以下是我如何编码回显数据

<?php

if (isset($playerID) && $playerID == 1)
{
    echo '<p class="playerInfoL">' . $player_bio . '</p><p class="playerAchievesL">' . $achievements . '</p>';
}

?>

我没有从php返回任何错误,但数据没有显示任何内容...请帮助...非常感谢你

2 个答案:

答案 0 :(得分:2)

这可能是因为当只有一条记录时,您将获取两次数组。您会看到,一旦结果用完了要获取的记录,后续提取将返回false。删除对mysql_fetch_array的第一个电话,您应该好好去。


要回答您的评论中的问题,您可以采取以下措施:

<?php

session_start();

require_once('config.php');

    //create query statement
    $query = 'SELECT * FROM player_info';

    //make the query from db
    $myData = mysql_query($query, $conn)
    OR exit('Unable to select data from table');

    while($row = mysql_fetch_array($myData)) {

        $playerID               = $row['id'];
        $player_bio             = $row['player_bio'];
        $achievements           = $row['player_achts'];
        echo '<p class="playerInfoL">' . $player_bio . '</p><p class="playerAchievesL">' . $achievements . '</p>';

}


?>

答案 1 :(得分:0)

试试这个:

while($row = mysql_fetch_array($myData, MYSQL_ASSOC)){
     $playerID               = $row['id'];
        $player_bio             = $row['player_bio'];
        $achievements           = $row['player_achts'];

}