MySql database query results use a table to output PHP code samples, mysql query results

Source: Internet
Author: User

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");?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.