1

Im trying to loop through my database and displaying every row. I don't know what is wrong with my code but it is not displaying anything at all... Can anyone help?

<?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
}
?>

This is where im including this file:

<?php include 'fetch_players.php'; ?>
4
  • 1
    Do you get a 500? Are you running PHP 7? $name is not defined but that should only throw a warning (incorrect assigning here possibly $steamid = $row["name"];). Do you have a database connection? Commented Apr 19, 2016 at 18:40
  • stop using deprecated mysql_* use mysqli_* or PDO Commented Apr 19, 2016 at 18:42
  • 2
    you're just assuming the query succeeded. bad assumption. never EVER assume success. mysql_query(...) or die(mysql_error()) should be the absolute barebones/minimum acceptable error handling. and even then, you shouldn't be using the mysql_*() functions anymore. they're dead/gone and should be avoided at all costs. Commented Apr 19, 2016 at 18:44
  • Thank you both for ur answers. Have updated it and Anant u were correct! Thank you so much! Commented Apr 19, 2016 at 19:36

1 Answer 1

1

An example of using mysqli_* with correct approach is given below. Please take care of comments too:-

<?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 }}?>
Sign up to request clarification or add additional context in comments.

3 Comments

This fixed my issue. Thank you @Anant :)
What do you mean with this tho: // assign values id wise to the array
means create array index based on row id's which is always unique, so no over-write will happen in any case and you will get all data

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.