This post is about data retrieving from MySQL database table using PHP. The bellow PHP script will give you HTML table output using MySQL table data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
<?php /* create a database 'test', and run this CREATE TABLE `contact_book` ( `id` int(11) NOT NULL AUTO_INCREMENT, `first_name` varchar(255) DEFAULT NULL, `last_name` varchar(255) DEFAULT NULL, `contact_number` varchar(255) DEFAULT NULL, `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; */ ?> <!DOCTYPE html > <html > <head> <title></title> </head> <body> <?php //connect to mysql server with host,username,password //if connection fails stop further execution and show mysql error $connection=mysql_connect('localhost','root','') or die(mysql_error()); //select a database for given connection //if database selection fails stop further execution and show mysql error mysql_select_db('test',$connection) or die(mysql_error()); //execute a mysql query to retrieve all the users from users table //if query fails stop further execution and show mysql error $query=mysql_query("SELECT * FROM contact_book") or die(mysql_error()); //if we get any results we show them in table data if(mysql_num_rows($query)>0): ?> <table> <tr> <td align="center">Id</td> <td align="center">First Name</td> <td align="center">Last Name</td> <td align="center">Contact Number</td> <td align="center">Created</td> </tr> <?php // looping while($row=mysql_fetch_object($query)):?> <tr> <td align="center"><?php echo $row->id; //row id ?></td> <td align="center"><?php echo $row->first_name; // row first name ?></td> <td align="center"><?php echo $row->last_name; //row last name ?></td> <td align="center"><?php echo $row->contact_number; //row contact number ?></td> <td align="center"><?php echo $row->created; //row created time ?></td> </tr> <?php endwhile;?> </table> <?php // no result show else: ?> <h3>No Results found.</h3> <?php endif; ?> </body> </html> |