php - Looping through and displaying multiple table rows from database sql -
im trying loop through database , displaying every row. don't know wrong code not displaying @ all... can help?
<?php $players = mysql_query("select * 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 im including file:
<?php include 'fetch_players.php'; ?>
an example of using mysqli_*
correct approach given below. please take care of comments too:-
<?php error_reporting(e_all); // check type of error ini_set('display_errors',1); // display error $connection = mysqli_connect('hostname','username','password','dbname'); // provide db credentials here $final_data = array(); // create empty array if($connection){ $players = mysqli_query($connection,"select * users"); if($players){ while ($row = mysqli_fetch_assoc($players)) { $final_data[$row['id']]['name'] = $row['name']; // assign values id wise 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 value or not? foreach($final_data $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 }}?>
Comments
Post a Comment