Jump to content

Recommended Posts

I have a MySQL query that works, as it displays the results I want in the database, however when I put that query into some php code it ignores the first row and I was wondering if someone could point out what I am doing wrong. 

 

This is for a fantasy football league.  You choose your team name from a drop down list and hit submit.  It is then supposed to produce a list of your choosen teams performance against all other active teams AND the results of the last game played. 

 

QUERY:

<?php
//the connection information is located in a function
include_once('call function');
$con = mysql_connect($hostname, $username, $password)
OR DIE ('Unable to connect to database! Please try again later.');
$db = mysql_select_db($dbname, $con);
$thing = $_GET['thing'];
$query = "select selected.teamname AS selected_team, selected_score.score AS selected_score, week.week,
year, home_id, ".
"target_score.score as target_score, target.teamname as targetname, week.ID ".
"from owners as selected ".
"JOIN game_scores AS selected_score ON selected.owner_id = selected_score.team_id ".
"JOIN game_setup ON game_setup.game_id = selected_score.game_id ".
"JOIN game_scores AS target_score ON target_score.game_id = game_setup.game_id AND
target_score.team_id != ". "selected_score.team_id ".
"JOIN owners AS target ON target.owner_id = target_score.team_id ".
"JOIN week ON week.week = game_setup.week ".
"WHERE selected.owner_id = $thing ".
"and target.active = 1 ".
"GROUP BY target.teamname, year, week.ID ";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
if (!$result) {
die('Invalid query: ' . mysql_error());
}

 

Now I know the query produces the correct results as I have run it many times in my database software and it always gives me all the rows.

 

QUERY RESULTS WITH LIMIT TOP 6:

selected_team|selected_score|week|year|home_id|target_score|targetname|ID

Xanadu Dragons|397.4|Week 9|2010|12|394.7|America Enforcers|9

Xanadu Dragons|357.4|Round 1|2010|1|361.6|America Enforcers|22

Xanadu Dragons|416.6|Week 2|2011|12|369.8|America Enforcers|2

Xanadu Dragons|417.0|Week 5|2010|11|301.9|Battle Masters|5

Xanadu Dragons|360.8|Week 4|2011|11|459.1|Battle Masters|4

Xanadu Dragons|99.0|Week 1|2006|1|93.0|Camelot Fluffy Butts|1

 

 

So the problem must be when I put the query results into the php code below:

$wins=0;
$losses=0;
$draws=0;
$last_target = false;
echo '<h3>' . $row['selected_team'] . '</h3>';
while ($row = mysql_fetch_assoc($result))
{
if ($last_target['targetname'] != $row['targetname'])
{
if ($last_target)
{
printf('
<table class="style1" border="1" width="400">
<tr>
<th>Opponent Name:</th>
<th colspan="2">%s</th>
</tr>
<tr>
<th>Wins</th>
<th>Losses</th>
<th>Draws</th>
</tr>
<tr>
<td align="center">%d</td>
<td align="center">%d</td>
<td align="center">%d</td>
</tr>
<tr>
<th>Last Game:</th>
<td colspan="2">%s of %s: %.1f - %.1f</td>
<tr>
</table><br/>',
$last_target['targetname'],
$wins,
$losses,
$draws,
$last_target['week'],
$last_target['year'],
$last_target['selected_score'],
$last_target['target_score']
);
}
$wins = $losses = $draws = 0;
}
if ($row['selected_score'] < $row['target_score'])
++$losses;
elseif ($row['selected_score'] == $row['target_score'])
++$draws;
else
++$wins;
$last_target = $row;
}
if ($last_target)
{
}
?>

 

The display however gives me the following:

 

Opponent Name: America Enforcers

Wins Losses Draws

1             1     0

Last Game: Week 2 of 2011: 416.6 - 369.8

 

as you can see from the above database results this team played the America Enforcers 3 times, however the php code only displays 2 games.

 

All other match ups are correct, it is just this first row.

 

Any ideas?

 

Please and thank you for your time and effort.

Link to comment
https://forums.phpfreaks.com/topic/253426-php-loop/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.