MySql database query results use table output PHP code example, mysql query results_PHP tutorial

WBOY
Release: 2016-07-13 10:01:58
Original
998 people have browsed it

MySql database query results use table output PHP code example, mysql query results

In general websites, we will usually see that the data of many database tables are displayed in the browser They all appear in the table, which makes me feel amazing at first, but if you think about it carefully, it is not too complicated. Since you can return dql and dml in general, it should not be a problem to return it in the form of a table. However, there is one point to explain. What is wrong is 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, we can only consider it on the server side. Think about the way to solve the problem, that is, print when returning Table labels and corresponding attributes and attribute values. Although this method seems unreasonable, it is also the most effective method. The specific code is as follows:

<&#63;php
//在表格中显示表的数据,常用方式
	function ShowTable($table_name){
		$conn=mysql_connect("localhost","root","toor");
		if(!$conn){
			echo "连接失败";
		}
		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);//获取行数
		$colums=mysql_num_fields($res);//获取列数
		echo "test数据库的"."$table_name"."表的所有用户数据如下:<br/>";
		echo "共计".$rows."行 ".$colums."列<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");
&#63;>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/970990.htmlTechArticleMySql database query results use a table to output PHP code examples. In general websites, we will usually look at the mysql query results. Yes, the data of many database tables appear in the browser...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!