radleybobins
radleybobins

Reputation: 981

Only one result showing after sql request and while loop

For some reason this only shows the last result, instead of showing all. The SQL works in the workbench, and $roommate is escaped, but the code has been trimmed for posting purposes:

$sql = "SELECT CONCAT(clients.studentFirstName, ' ', clients.studentLastName) AS name, appointments.location, appointments.subLocation, appointments.startTime, appointments.endTime, appointments.date
FROM appointments JOIN clients
ON appointments.clientID = clients.clientID
WHERE CONCAT(clients.studentFirstName, ' ', clients.studentLastName) = '".$roommate."';";

while ($row = mysql_fetch_array($result)) { 
    echo
    '<table>
        <tr>
            <td>'
                .$row["name"].
            '</td>
            <td>'
            .$row["location"].
            '</td>
            <td>'
                .$row["subLocation"].
            '</td>
        </tr>
        <tr>
            <td>'
                .$row["startTime"].
            ' - </td>
            <td>'
                .$row["endTime"].
            '</td>
            <td>'
                .$row["date"].
            '</td>
        </tr>
    </table>';

}

Upvotes: 0

Views: 615

Answers (1)

Trott
Trott

Reputation: 70183

Use mysql_num_rows() to determine the number of rows that were returned by your query. If it reports that you are only getting 1 result, then you will need to refine your query to get the number of results you intend.

If you're using one of the mysql_fetch_* functions before the while loop, that would advance the cursor and make you miss one or more results in your while loop. If that's the case, call mysql_data_seek($result, 0) before the while loop.

Upvotes: 1

Related Questions