Rumah >pembangunan bahagian belakang >tutorial php >PHP如何实现MYSQL查询功能

PHP如何实现MYSQL查询功能

小云云
小云云asal
2018-03-15 15:12:3510192semak imbas

本文主要和大家分享PHP如何实现MYSQL查询功能,本文主要以代码的形式和大家分享,希望能帮助到大家。

相关mysql视频教程推荐:《mysql教程

展示数据库

<?php
header(&#39;Content-Type: text/html;charset=utf-8&#39;);
$host = "127.0.0.1";
$port = &#39;3306&#39;;
$user = &#39;root&#39;;
$pass = &#39;123654&#39;;
$charset = &#39;utf8&#39;;  //设置默认字符
$link = mysql_connect("$host:$port",$user,$pass);  //链接数据库
$sql = &#39;show databases&#39;;  //展示数据sql代码
if(!$result = mysql_query($sql,$link)){  //执行数据库代码
	echo &#39;SQL执行失败&#39;.&#39;<br>&#39;;
	echo "出错的地方是:".$sql.&#39;<br>&#39;;
	echo "错误代码是:",mysql_errno($this->link).&#39;<br>&#39;;
	echo "错误信息是:",mysql_error($this->link).&#39;<br>&#39;;
	die();
}
echo &#39;<table>&#39;;
 //mysql_fetch_assoc()  从结果集中取得一行作为关联数组 
while($row = mysql_fetch_assoc($result)){
	// var_dump($row);
	echo &#39;<tr>&#39;;
	echo &#39;<td>&#39;.&#39;<a href = "table.php?dbname=&#39;.$row[&#39;Database&#39;].&#39;">&#39;;
	//用的是字符串链接的方式 连接跳转的地方
	echo $row[&#39;Database&#39;];  //输出数据库名
	echo &#39;</a>&#39;.&#39;</td>&#39;;
	echo &#39;</tr>&#39;;
}
echo &#39;</table>&#39;;



执行完sql语句之后返回的就是结果集;


实现结果如下:


展示表

<?php
header(&#39;Content-Type: text/html;charset=utf-8&#39;);
$host = "127.0.0.1";
$port = &#39;3306&#39;;
$user = &#39;root&#39;;
$pass = &#39;123654&#39;;
$charset = &#39;utf8&#39;;
$link = mysql_connect("$host:$port",$user,$pass);
mysql_query(&#39;set names &#39;.$charset,$link);
$dbname = $_GET[&#39;dbname&#39;];
$sql = "use `$dbname`";
mysql_query($sql,$link);
$sql = &#39;show tables&#39;;  //展示表的sql语句
if(!$result = mysql_query($sql,$link)){
	echo &#39;SQL执行失败&#39;.&#39;<br>&#39;;
	echo "出错的地方是:".$sql.&#39;<br>&#39;;
	echo "错误代码是:",mysql_errno($this->link).&#39;<br>&#39;;
	echo "错误信息是:",mysql_error($this->link).&#39;<br>&#39;;
	die();
}
echo &#39;<table>&#39;;
while($row = mysql_fetch_assoc($result)){
	 var_dump($row);
	echo &#39;<tr>&#39;;
	echo &#39;<td>&#39;;
	echo $row[&#39;Tables_in_&#39;.$dbname];
	echo &#39;</td>&#39;;

	echo &#39;<td>&#39;.&#39;<a href = "column.php?tablename=&#39;.$row[&#39;Tables_in_&#39;.$dbname].&#39;&dbname=&#39;.$dbname.&#39;">&#39;;
	echo &#39;结构&#39;;
	echo &#39;</a>&#39;.&#39;</td>&#39;;

	echo &#39;<td>&#39;.&#39;<a href = "data.php?tablename=&#39;.$row[&#39;Tables_in_&#39;.$dbname].&#39;&dbname=&#39;.$dbname.&#39;">&#39;;
	echo &#39;数据&#39;;
	echo &#39;</a>&#39;.&#39;</td>&#39;;
	echo &#39;</tr>&#39;;
}
echo &#39;</table>&#39;;

表中的结构展示

展示表中的信息的时候,至少要传递两个参数,第一个是数据库的名字,第二个是表的名称

这样才能根据上面的两个信息找出表中的信息

<?php
header(&#39;Content-Type: text/html;charset=utf-8&#39;);
$host = "127.0.0.1";
$port = &#39;3306&#39;;
$user = &#39;root&#39;;
$pass = &#39;123654&#39;;
$charset = &#39;utf8&#39;;
$link = mysql_connect("$host:$port",$user,$pass);
mysql_query(&#39;set names &#39;.$charset,$link);
$dbname = $_GET[&#39;dbname&#39;];
$tablename = $_GET[&#39;tablename&#39;];
$sql = "desc `$dbname`.`$tablename`";  //展示出表的信息
if(!$result = mysql_query($sql,$link)){
	echo &#39;SQL执行失败&#39;.&#39;<br>&#39;;
	echo "出错的地方是:".$sql.&#39;<br>&#39;;
	echo "错误代码是:",mysql_errno($this->link).&#39;<br>&#39;;
	echo "错误信息是:",mysql_error($this->link).&#39;<br>&#39;;
	die();
}
echo &#39;<table>&#39;;
while($row = mysql_fetch_assoc($result)){
	 //var_dump($row);
	echo &#39;<tr>&#39;;
	echo &#39;<td>&#39;;
	echo $row[&#39;Field&#39;];  //输出名称
	echo &#39;</td>&#39;;

	echo &#39;<td>&#39;;
	echo $row[&#39;Type&#39;];  //输出类型
	echo &#39;</td>&#39;;
	echo &#39;</tr>&#39;;
}
echo &#39;</table>&#39;;

表中数据的展示

<?php
header(&#39;Content-Type: text/html;charset=utf-8&#39;);
$host = "127.0.0.1";
$port = &#39;3306&#39;;
$user = &#39;root&#39;;
$pass = &#39;123654&#39;;
$charset = &#39;utf8&#39;;
$link = mysql_connect("$host:$port",$user,$pass);
mysql_query(&#39;set names &#39;.$charset,$link);

$dbname = $_GET[&#39;dbname&#39;];//获得数据库名
$tablename = $_GET[&#39;tablename&#39;];//获得表名

//选择某认数据库
$sql = "use `$dbname`";
if(!$result = mysql_query($sql,$link)){
	echo &#39;SQL执行失败&#39;.&#39;<br>&#39;;
	echo "出错的地方是:".$sql.&#39;<br>&#39;;
	echo "错误代码是:",mysql_errno($this->link).&#39;<br>&#39;;
	echo "错误信息是:",mysql_error($this->link).&#39;<br>&#39;;
	die();
}
//获得字段信息
$sql = "desc `$tablename`";
if(!$result = mysql_query($sql,$link)){
	echo &#39;SQL执行失败&#39;.&#39;<br>&#39;;
	echo "出错的地方是:".$sql.&#39;<br>&#39;;
	echo "错误代码是:",mysql_errno($this->link).&#39;<br>&#39;;
	echo "错误信息是:",mysql_error($this->link).&#39;<br>&#39;;
	die();
}

echo &#39;<table border="1">&#39;;
//展示字段
echo "<tr>";
while($rows = mysql_fetch_assoc($result)){
	echo "<th>";
	echo $rows[&#39;Field&#39;];
	echo "</th>";
}
echo "</tr>";
//查询每个表的数据
$sql = "select * from `$tablename` where 1";
if(!$result = mysql_query($sql,$link)){
	echo &#39;SQL执行失败&#39;.&#39;<br>&#39;;
	echo "出错的地方是:".$sql.&#39;<br>&#39;;
	echo "错误代码是:",mysql_errno($this->link).&#39;<br>&#39;;
	echo "错误信息是:",mysql_error($this->link).&#39;<br>&#39;;
	die();
}

//遍历结果集 得到所有字段
while($rows = mysql_fetch_assoc($result)){
	echo &#39;<tr>&#39;;
	foreach ($rows as $value) {
		echo &#39;<td>&#39;;
		echo $value === &#39;&#39; ?&#39; &#39;:($value===NULL?&#39;NULL&#39;:$value);
		//三目运算符的嵌套
		echo &#39;</td>&#39;;
	}
	echo &#39;</tr>&#39;;
}
echo &#39;</table>&#39;;

相关推荐:

MySQL查询语句之复杂查询

MySQL查询时间基础教程

Mysql查询结果顺序按in()中ID的顺序排列的实例分析

Atas ialah kandungan terperinci PHP如何实现MYSQL查询功能. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn