Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 isnt 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, thanks.

share|improve this question
1  
whats the error? –  messi fan Jun 14 at 5:43
1  
please use fetch_assoc.if anybody change order of mysql column, it will cause error –  messi fan Jun 14 at 5:47
add comment

3 Answers

up vote 3 down vote accepted
 $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.

share|improve this answer
 
It will not work –  Amit Garg Jun 14 at 5:47
 
what wrong with that? –  Naeem Jun 14 at 5:48
2  
no there are mistakes tr and td, see carefully, did u see that <td>$data[2]</td></tr><td>$data[3]</td> –  Naeem Jun 14 at 5:50
 
Nomi is right. I was wondering myself why everyone was pasting all those trs. –  MaX Jun 14 at 5:51
add comment

When you are echoing array items it should be

echo "<tr><td>" . $data[0] . "</td></tr>";
share|improve this answer
add comment
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>";

Use braces.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.