user2484801 user2484801 - 1 month ago 10
MySQL Question

PHP array error in displaying a table

I am trying to display all the data from a mysql table into a html table and the PHP is getting the data fine but the table isn't displaying it properly this is my code:

<table border="1">
<tr>
<td>ID</td>
<td>Name</td>
<td>Phone Number</td>
<td>Email</td>
<td>Time</td>
<td>Number Of People</td>
<td>Time Placed</td>
</tr>
<?php

$host = "localhost";
$database = "reservation";
$user = "root";
$pass = "root";

//connection to the database
mysql_connect($host, $user, $pass)
or die ('cannot connect to the database: ' . mysql_error());

//select the database
mysql_select_db($database)
or die ('cannot select database: ' . mysql_error());

$sql = "SELECT * FROM reservation ORDER BY timeplaced";
$result = mysql_query($sql);
while($data = mysql_fetch_row($result)){

echo("<tr><td>$data[0]</td><td>$data[1]</td><td>$data[2]</td></tr><td>$data[3]</td></tr><td>$data[4]</td></tr><td>$data[5]</td></tr><td>$data[6]</td></tr>");

}



?>
</table>


Please help.

Answer
 $sql = "SELECT * FROM reservation ORDER BY timeplaced";
 $result = mysql_query($sql);
 while($data = mysql_fetch_row($result)){

 echo("<tr>
    <td>$data[0]</td>
    <td>$data[1]</td>
    <td>$data[2]</td>
    <td>$data[3]</td>
    <td>$data[4]</td>
    <td>$data[5]</td>
    <td>$data[6]</td>
</tr>");

 }

when you use TDs in TR, they all must be same numbers, or use colspan, try the above echo statement, it will display your table properly.

Comments