Jump to content

[SOLVED] Need Help With This One


refiking

Recommended Posts

I am trying to echo all of the winners from the db.  How can I show all of them.  Right now, it only shows the last entry in the table.  Here is my code as is:

 


include 'header.php';
include 'cont.php';
$sql = mysql_query("SELECT * FROM scores");
while ($row = mysql_fetch_assoc($sql)) {
$hscore = $row['homescore'];
$ascore = $row['awayscore'];
$home = $row['home'];
$away = $row['away'];
$timestamp = $row['timestamp'];
$round = $row['roundname']; 
}
IF ($hscore >= $ascore) {
$winner = $home;
}
IF ($ascore >= $hscore) {
$winner = $away;
}
Echo $winner;

Link to comment
https://forums.phpfreaks.com/topic/87799-solved-need-help-with-this-one/
Share on other sites

Try

 

<?php

include 'header.php';
include 'cont.php';

$sql = mysql_query("SELECT * FROM scores");

while ($row = mysql_fetch_assoc($sql)) {

   $hscore = $row['homescore'];
   $ascore = $row['awayscore'];
   $home = $row['home'];
   $away = $row['away'];
   $timestamp = $row['timestamp'];
   $round = $row['roundname'];
    
   if ($hscore >= $ascore) {
       $winner = $home;
   }
   if ($ascore >= $hscore) {
       $winner = $away;
   }

   echo $winner.'<br>';

}

?>

Well, then there is only one record in the table. Run this code, and copy/paste the output.

 

<?php
  include 'header.php';
  include 'cont.php';

  $sql = mysql_query("SELECT * FROM scores");
  print "Row Count: ".mysql_num_rows($sql)."<br>";

  while ($row = mysql_fetch_assoc($sql)) {
    $hscore = $row['homescore'];
    $ascore = $row['awayscore'];
    $home = $row['home'];
    $away = $row['away'];
    $timestamp = $row['timestamp'];
    $round = $row['roundname'];
    
    if($hscore > $ascore){
      $winner = $home;
    }elseif($ascore > $hscore){
      $winner = $away;
    }else{
      $winner = "TIE";
    }
    echo "Winner: $winner<br>";
  }
?>

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.