1

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

2
  • After your "$row = mysql_fetch_array($myData, MYSQL_ASSOC);" line please add a var_dump($row); and comment what shows up. Commented Aug 13, 2011 at 4:43
  • it showed up the whole data from table in database, and start with this "array(11) { ["id"]=> string(1) "1" ["time_period"]=> string(9) "1899-1903" ["player_name"]=> string(11) "Joan Camper" ["player_bio"]=> string(780)......" Commented Aug 13, 2011 at 4:46

2 Answers 2

2

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

}


?>
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you, i did not know why I put it twice right there... it worked ... thanks again
Not a problem; I'm happy to help. You can accept the answer by clicking the check mark, if you wish.
One more question thou... you see my second part of the code ... I just to use it and set the $playerID == 2, to get the data from second row and display in another div ... but it is not there nor give me any error ... any idea?
This is because you're fetching one record only, and that one is whatever happens to be first, and it isn't player 2, so nothing happens. You may want to loop the fetches.
ok ... now .. dumb question .. when i use "while(isset($mydata))", then i got fatal error ... maximum 30 seconded exceeded ... can you help me out one more time? How should I loop ?
|
0

Try this:

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.