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 c开发者_运维技巧ode 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
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>';
}
?>
Try this:
while($row = mysql_fetch_array($myData, MYSQL_ASSOC)){
$playerID = $row['id'];
$player_bio = $row['player_bio'];
$achievements = $row['player_achts'];
}
精彩评论