How PHP implements MYSQL query function

小云云
Release: 2023-03-21 14:26:02
Original
10160 people have browsed it

This article mainly shares with you how PHP implements the MYSQL query function. This article mainly shares it with you in the form of code. I hope it can help you.

Related mysql video tutorial recommendations: "mysql tutorial"

Display database

<?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;;
Copy after login



After executing the sql statement, the result set is returned;


The results are as follows:


Display table

<?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;;
Copy after login

Structure display in the table

When displaying the information in the table, at least two parameters must be passed, the first is the name of the database, and the second is The name of the table

In this way, the information in the table can be found based on the above two information

<?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;;
Copy after login

Display of data in the table

<?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;;
Copy after login

Related recommendations:

Complex query of MySQL query statement

MySQL query time basic tutorial

Mysql query results are ordered according to the ID in in() Example analysis of sequential arrangement

The above is the detailed content of How PHP implements MYSQL query function. For more information, please follow other related articles on the PHP Chinese website!

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