I am having issues with my PHP while statement. It is:
$row = mysqli_fetch_array($result);
$time_in_12_hour_format = date("g:i a", strtotime($row['Time']));
while($row = mysqli_fetch_array($result)){ //Creates a loop to loop through results
echo "<tr><td>" . $row['Name'] . "</td><td>" . $row['Description'] . "</td><td>" . $row['Date'] . " at " . $time_in_12_hour_format . "</td><td>" . $row['Location'] . "</td>"; //$row['index'] the index here is a field name
Yes, I do realize I'm defining $row twice, but I'm not sure how to change that. If I remove $row = mysqli_fetch_array($result) from the while statement, the page will not load and it returns errors. As of now, the page loads but the table is empty, even though I know for a fact there is one record in there that was displaying in the table before I added my military time converter. Any help is appreciated.
Try this
while ($row = mysqli_fetch_array($result)) { //Creates a loop to loop through results
$time_in_12_hour_format = date("g:i a", strtotime($row['Time']));
echo "<tr><td>" . $row['Name'] . "</td><td>" . $row['Description'] . "</td><td>" . $row['Date'] . " at " . $time_in_12_hour_format . "</td><td>" . $row['Location'] . "</td>";
}
Every time you call mysqli_fetch_array
it fetches a row from table, so since you said your table has one record, the one record will be returned in the first mysqli_fetch_array
call and eventually the second call to mysqli_fetch_array
returns NULL
since there is no more records to fetch in the table
Try moving the $time_in_12_hour_format
into the while loop.
while($row = mysqli_fetch_array($result)){ //Creates a loop to loop through results
$time_in_12_hour_format = date("g:i a", strtotime($row['Time']));
echo "<tr><td>" . $row['Name'] . "</td><td>" . $row['Description'] . "</td><td>" . $row['Date'] . " at " . $time_in_12_hour_format . "</td><td>" . $row['Location'] . "</td>"; //$row['index'] the index here is a field name
}
This will also solve defining $row twice, which is what causes the issue in the first place. Because you already called mysqli_fetch_array($result)
your while loop is starting at row 2.
The $time_in_12_hour_format
should be set for each row, right? If yes, then try to move it inside the while loop. Your current code will only start to show row 2 and row1 will be ignored. The modified code should look like:
while($row = mysqli_fetch_array($result)){ //Creates a loop to loop through results
$time_in_12_hour_format = date("g:i a", strtotime($row['Time']));
echo "<tr><td>" . $row['Name'] . "</td><td>" . $row['Description'] . "</td><td>" . $row['Date'] . " at " . $time_in_12_hour_format . "</td><td>" . $row['Location'] . "</td>"; //$row['index'] the index here is a field name
}