我试图遍历我的数据库并显示每一行。我不知道我的代码有什么问题,但它根本没有显示任何内容......任何人都可以帮忙吗?
<?php
$players = mysql_query("SELECT * FROM users");
while ($row = mysql_fetch_assoc($players)) {
$steamid = $row["name"];
$profilename = $row["profilename"];
$profileurl = $row["profileurl"];
$avatar = $row["avatar"];
$region = $row["region"];
?>
<p><?php echo $name ?></p>
<p>><?php echo $profilename ?></p>
<p>><?php echo $profileurl ?></p>
<p><?php echo $avatar ?></p>
<?php
}
?>
这是包含此文件的地方:
<?php include 'fetch_players.php'; ?>
答案 0 :(得分:1)
下面给出了使用mysqli_*
正确方法的示例。请注意评论: -
<?php
error_reporting(E_ALL); // check all type of error
ini_set('display_errors',1); // display those error
$connection = mysqli_connect('hostname','username','password','dbname'); // provide your db credentials here
$final_data = array(); // create empty array
if($connection){
$players = mysqli_query($connection,"SELECT * FROM users");
if($players){
while ($row = mysqli_fetch_assoc($players)) {
$final_data[$row['id']]['name'] = $row['name']; // assign values id wise to the array
$final_data[$row['id']]['profilename'] = $row['profilename'];
$final_data[$row['id']]['profileurl'] = $row['profileurl'];
$final_data[$row['id']]['avatar'] = $row['avatar'];
$final_data[$row['id']]['region'] = $row['region'];
}
}else{
echo "query execution failed because of". mysqli_error($connection);
}
}else{
echo "db connection error because of". mysqli_connect_error();
}
?>
<?php
if(count($final_data) >0){ // check array have some value or not?
foreach($final_data as $final_dat){?>
<p><?php echo $final_dat['name'] ?></p><!-- print out values -->
<p><?php echo $final_dat['profilename'] ?></p>
<p><?php echo $final_dat['profileurl'] ?></p>
<p><?php echo $final_dat['avatar'] ?></p>
<p><?php echo $final_dat['region'] ?></p>
<?php }}?>