Jump to content

PHP MySQL Issue


hosker

Recommended Posts

I have a query that runs perfectly when I enter it into the SQL tab within my PHP Admin screen, but when I add my PHP to it, it does not display all the values. Here is my code:

 

 

<?php
$i = 1;
$position = 1;
while($i < 39) {
$tournament_sql = "(SELECT * FROM weekly_picks, tournaments_2013, leaderboards_2013 WHERE tournaments_2013.tournament_id = leaderboards_2013.tournament_id AND weekly_picks.tournament_id = leaderboards_2013.tournament_id AND weekly_picks.player_id = leaderboards_2013.player_id AND tournaments_2013.id = '$i') ORDER BY (leaderboards_2013.player_money)DESC";
$tournament_result = mysql_query($tournament_sql);
$tournament_row = mysql_fetch_assoc($tournament_result);
print "<h2>" . $tournament_row['tournament'] . "</h2>";
print "<div id='picks'><table border=1><tr><th>Position</th><th>Player</th><th>Total</th></tr>";
while ($tournament_row = mysql_fetch_assoc($tournament_result)) {
echo "<tr><td>" . $position . "</td><td>" . $tournament_row['user'] . "</td><td>" . $tournament_row['player_money'] . "</td></tr>";
$position++;
}
print "</table></div>";
$i++;
$position = 1;
};
?>


Link to comment
https://forums.phpfreaks.com/topic/273257-php-mysql-issue/
Share on other sites

$tournament_row = mysql_fetch_assoc($tournament_result);

 

The first row from each set of data is missing. You are fetching the first row using the above line of code.

 

You need to do a couple of things -

 

1) Don't put a query inside of a loop. You need to execute ONE query that gets the rows you want in the order that you want them. Then you simply output the data the way you want it when you iterate over the rows.

 

2) To output the heading, you simply remember the last heading (initialize to a value that won't ever appear in the data, such as a null) and detect when it changes to output the new heading, then save the new heading value as the last heading.

Link to comment
https://forums.phpfreaks.com/topic/273257-php-mysql-issue/#findComment-1406265
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.