minhtu
minhtu

Reputation: 21

PHP not be able to echo data

I'm trying to get data from database, and echo them on the page using their unique id.. below is my code

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


?>

and here is how i code for echo the data

<?php

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

?>

I don't get any error return from php, but the data does not display anything... help please ... thank you so much

Upvotes: 1

Views: 116

Answers (2)

Ankur Mukherjee
Ankur Mukherjee

Reputation: 3867

Try this:

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

}

Upvotes: 0

Delan Azabani
Delan Azabani

Reputation: 81394

This may be because you're fetching the array twice, when there is only one record. You see, once the result runs out of records to fetch, subsequent fetches will return false. Remove the first call to mysql_fetch_array and you should be good to go.


To answer the question in your comments, this is how you may want to do things:

<?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>';

}


?>

Upvotes: 2

Related Questions