PHP/MySQL : Creating a guest book
Olivier Roble
Displaying the comments
Collecting comments from visitors is all very well, but useless if you cannot display them. We will use another SQL query
to retrieve and display the table data. This is what it looks like:
SELECT [columns] FROM [tablename] ORDER BY [sort_column]
In our example, we will display the collected comments sorted by date. And to improve the page layout and readability, we
will display them in a table. Here is our display script:
1 : <html>
2 : <head><title>PHP workshop for form management: adminimp.php</title><head>
3 : <body>
4 : <table align="center" cellspacing="0" cellpadding="0" border="1" width="80%">
5 : <tr>
6 : <td bgcolor="black"><font color="white">DATE</td>
7 : <td bgcolor="black"><font color="white">NAME</td>
8 : <td bgcolor="black"><font color="white">EMAIL</td>
9 : <td bgcolor="black"><font color="white">IMPRESSION</td>
10: <td bgcolor="black"><font color="white">COMMENTS</td>
11: </tr>
12: <?php
13: $db = mysql_connect();
14: $sql="SELECT * FROM impression ORDER BY date";
15: $res=mysql_query($sql, $db);
16: while ($ligne = mysql_fetch_object ($res))
17: {
18: print "<tr>";
19: print "<td>$ligne->date</td>";
20: print "<td>$ligne->nom</td>";
21: print "<td>$ligne->email</td>";
22: print "<td>$ligne->impression</td>";
23: print "<td>$ligne->comments</td>";
24: print "</tr>";
25: }
26: mysql_free_result ($res);
27: ?>
28: </table>
29: </body>
30: </html>
Our visitors' book is now finished.
As you can see, it is simple to implement. Besides, if you have read our workshop on creating a dynamic summary page, all
the instructions used to display query results should be familiar to you.