MySql database query results use a table to output PHP code samples, mysql query results
In general websites, we usually see that the table data in many databases is displayed in the table in the browser. It is amazing at first, but it is not too complicated to think about it carefully, since dql and dml can be returned in a table, it is not a problem. However, it is worth noting that it is wrong to design a script on the client to implement the problem, even if it can be implemented, it is very complicated. Therefore, you can only consider the server and think about how to solve the problem. That is, print the table tag and the corresponding attribute and attribute value when returning the result, although this method does not seem reasonable, it is also the most effective method. The specific code is as follows:
<? Php // displays table data in a table. Common methods include function ShowTable ($ table_name) {$ conn = mysql_connect ("localhost", "root", "toor"); if (! $ Conn) {echo "connection failed";} mysql_select_db ("test", $ conn); mysql_query ("set names utf8 "); $ SQL = "select * from $ table_name"; $ res = mysql_query ($ SQL, $ conn); $ rows = mysql_affected_rows ($ conn ); // obtain the number of rows $ colums = mysql_num_fields ($ res); // obtain the number of columns echo "test Database ". "$ table_name ". "All user data in the table is as follows: <br/>"; echo "Total ". $ rows. "line ". $ colums. "column <br/>"; echo "<table style = 'border-color: # efefef; 'border = '1px 'cellpadding = '5px 'cellspacing = '0px '> <tr > "; For ($ I = 0; $ I <$ colums; $ I ++) {$ field_name = mysql_field_name ($ res, $ I ); echo "<th> $ field_name </th>";} echo "</tr>"; while ($ row = mysql_fetch_row ($ res )) {echo "<tr>"; for ($ I = 0; $ I <$ colums; $ I ++) {echo "<td> $ row [$ I] </td>" ;}echo "</tr>" ;}echo "</table> ";} showTable ("test1");?>