Maintaining a youth sports league website with schedules and standings that are displayed via a HTML front end and use PHP to communicate with a MySQL back end database. All has worked well for several years, but I'm having a big problem recently with scores being reported multiple times inadvertently and I'm trying to close the loophole that allows it to happen.
Below is a small excerpt of PHP code that I'm trying to add to the score reporting script. The idea is to query the MySQL database and see if the home and away score of the selected game are currently "0". If they are, the score has not yet been reported and the script can execute. However, if they're any other value, then I want the script to stop.
When testing, the script is terminated with the "already reported" message regardless of whether or not the game has been reported. I believe I have an issue with the comparison operators I'm attempting to implement. Can anyone point out something obvious about the code that I seem to be missing? A little guidance would be GREATLY appreciated!
// Check to see if game has already been reported:
$qh = "SELECT home_score FROM $table WHERE game_id=$row[0]";
$hscore = mysqli_query($db, $qh);
$qa = "SELECT away_score FROM $table WHERE game_id=$row[0]";
$ascore = mysqli_query($db, $qa);
if ($hscore != 0 || $ascore != 0) {
echo "The scores for this game have already been reported. Please try refreshing your browser on the schedule page to verify.";
} else {
Just write 1 query to fetch both scores, then you can easily compare them to display what you're after.
// Check to see if game has already been reported:
$q = "SELECT home_score, away_score FROM $table WHERE game_id=$row[0]";
$result = mysqli_query($db, $q);
$scores = $result->fetch_assoc();
if ($scores['home_score'] != 0 || $scores['away_score'] != 0) {
echo "The scores for this game have already been reported. Please try refreshing your browser on the schedule page to verify.";
} else {
The fetch_assoc()
method fetches the first row of the result that comes back from MySQL as an associative array, which makes it nice and easy to use. :)