MySQL database query results with table output PHP code example _php tips

Source: Internet
Author: User
Tags php code

In a general web site, we will usually see that many of the tables in the database in the browser are in the table, the first to feel very magical, but careful thinking is not too complicated, since you can dql and DML general return, the form of return should not be a problem, but, One thing is that it's wrong to design a script on the client to implement the problem. Even if it can be implemented is very complex, so, only on the server side to consider, think about the way to solve the problem, that is, when the return of the print table tags and corresponding attributes and attribute values, although this approach does not seem reasonable, But this is also the most effective way. The specific code is as follows:

 <?php//display table data in tables, common style 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);//Get the number of rows $colums =mysql_num_fields ($res);//Get the number of columns echo "Test Database". $table _name "."
		Table of all user data 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.