0

I know this has been asked before and I have done the research but just cant seem to get it right.

Basically I am trying to extract the raceName from the Array but when I add the the line of code it removes the first line from the while query result.

I know its related to the two mysqli_fetch_array statements but I am looking for some direction.

<?php 

        $id = $_GET['id'];
        if($id == "") {

            echo "Race is unavailable";
            echo "<br>";

        } else {

            $result = mysqli_query($mysqli, "SELECT RaceEvent.raceName, horse.horseName, RaceData.raceCode, RaceData.tabNumber, RaceData.barrier, horse.horseCode, Jockey.jockeyName
            FROM ((((RaceMeet 
                INNER JOIN RaceEvent 
                    ON RaceEvent.meetCode=RaceMeet.meetCode) 
                INNER JOIN RaceData 
                    ON RaceEvent.raceCode=RaceData.raceCode) 
                INNER JOIN horse 
                    ON horse.horseCode=RaceData.horseCode) 
                INNER JOIN jockey
                    ON jockey.jockeyCode=RaceData.jockeyCode)
            WHERE RaceEvent.raceCode = '$id'");

        $row = mysqli_fetch_array($result);
            printf ("<h3>%s \n </h3>", $row['raceName']);

        while ($row = mysqli_fetch_array($result)) { 

            echo "<tr>";
                echo "<td>" . $row['horseCode'] . "</td>";
                echo "<td>" . $row['horseName'] . "</td>";
                echo "<td>" . $row['jockeyName'] . "</td>";
                echo "<td>" . $row['tabNumber'] . "</td>";
                echo "<td>" . $row['barrier'] . "</td>";
        }                
     }
   ?>

This line is meant to be the race name and is only meant to be returned once:

$row = mysqli_fetch_array($result);
            printf ("<h3>%s \n </h3>", $row['raceName']);

Whilst the while loop is meant to return the number of rows, pending the number of horses in the race:

while ($row = mysqli_fetch_array($result)) { 

            echo "<tr>";
                echo "<td>" . $row['horseCode'] . "</td>";
                echo "<td>" . $row['horseName'] . "</td>";
                echo "<td>" . $row['jockeyName'] . "</td>";
                echo "<td>" . $row['tabNumber'] . "</td>";
                echo "<td>" . $row['barrier'] . "</td>";
        }

I appreciate your time an help

Shadow
  • 33,525
  • 10
  • 51
  • 64
MdM
  • 81
  • 2
  • 9
  • 1
    Use [mysqli_fetch_assoc](http://php.net/manual/en/mysqli-result.fetch-assoc.php) instead of `mysqli_fetch_array`. Also, your code is open to SQL injection. Please use [Prepared statements](https://stackoverflow.com/a/60496/2469308) – Madhur Bhaiya Sep 22 '18 at 13:05
  • Thank you Madhur, code is in infant stage and will sort that out. – MdM Sep 22 '18 at 23:02

2 Answers2

0

After you read first line to recieve only race number, you dont echo data from this first result.. You need before while loop place seek first row of result with

$result->data_seek(0);

And when you get you first row from table with data.

Vladimir
  • 99
  • 8
0

As your while() loop reads the data at the start, this will ignore the data already read. You could instead use a do...while() loop which will read the next row at the end.

$row = mysqli_fetch_assoc($result);
printf ("<h3>%s \n </h3>", $row['raceName']);

do { 
    echo "<tr>";
    echo "<td>" . $row['horseCode'] . "</td>";
    echo "<td>" . $row['horseName'] . "</td>";
    echo "<td>" . $row['jockeyName'] . "</td>";
    echo "<td>" . $row['tabNumber'] . "</td>";
    echo "<td>" . $row['barrier'] . "</td>";
    echo "</tr>";   // Don't forget to close the row
}
while ($row = mysqli_fetch_assoc($result));
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55