The following PHP code will collect the data from users table and show in HTML table:
I have given inline code comments so that you can understand everything. Please visit my another tutorial in this series to display mysql data in php with pagination .
<?php /* create a database 'test',and execute these two query to run the demo CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `fname` varchar(255) DEFAULT NULL, `lname` varchar(255) DEFAULT NULL, `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /*Data for the table `users` */ /*insert into `users`(`id`,`fname`,`lname`,`created`) values (1,'Mahesh','Chari','2013-04-24 09:45:42'), (2,'Mahesh ','Guggilla','2013-04-24 09:45:55'), (3,'Chari','Mahesh','2013-04-24 09:46:01'); */ ?> <!DOCTYPE html > <html > <head> <title>Display Mysql Data in table with PHP</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 users") or die(mysql_error()); //if we get any results we show them in table data if(mysql_num_rows($query)>0): ?> <table width="100%" border="0"> <tr> <td align="center">Id</td> <td align="center">First Name</td> <td align="center">Last Name</td> <td align="center">Created</td> </tr> <?php //while we going through each row we display info while($row=mysql_fetch_object($query)):?> <tr> <td align="center"><?php echo $row->id; //row id ?></td> <td align="center"><?php echo $row->fname; // row first name ?></td> <td align="center"><?php echo $row->lname; //row las tname ?></td> <td align="center"><?php echo $row->created; //row created time ?></td> </tr> <?php endwhile;?> </table> <?php //if we can't get results we show information else: ?> <h3>No Results found.</h3> <?php endif; ?> </body> </html> |

