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('Content-Type: text/html;charset=utf-8'); $host = "127.0.0.1"; $port = '3306'; $user = 'root'; $pass = '123654'; $charset = 'utf8'; //设置默认字符 $link = mysql_connect("$host:$port",$user,$pass); //链接数据库 $sql = 'show databases'; //展示数据sql代码 if(!$result = mysql_query($sql,$link)){ //执行数据库代码 echo 'SQL执行失败'.'<br>'; echo "出错的地方是:".$sql.'<br>'; echo "错误代码是:",mysql_errno($this->link).'<br>'; echo "错误信息是:",mysql_error($this->link).'<br>'; die(); } echo '<table>'; //mysql_fetch_assoc() 从结果集中取得一行作为关联数组 while($row = mysql_fetch_assoc($result)){ // var_dump($row); echo '<tr>'; echo '<td>'.'<a href = "table.php?dbname='.$row['Database'].'">'; //用的是字符串链接的方式 连接跳转的地方 echo $row['Database']; //输出数据库名 echo '</a>'.'</td>'; echo '</tr>'; } echo '</table>';
After executing the sql statement, the result set is returned;
The results are as follows:
<?php header('Content-Type: text/html;charset=utf-8'); $host = "127.0.0.1"; $port = '3306'; $user = 'root'; $pass = '123654'; $charset = 'utf8'; $link = mysql_connect("$host:$port",$user,$pass); mysql_query('set names '.$charset,$link); $dbname = $_GET['dbname']; $sql = "use `$dbname`"; mysql_query($sql,$link); $sql = 'show tables'; //展示表的sql语句 if(!$result = mysql_query($sql,$link)){ echo 'SQL执行失败'.'<br>'; echo "出错的地方是:".$sql.'<br>'; echo "错误代码是:",mysql_errno($this->link).'<br>'; echo "错误信息是:",mysql_error($this->link).'<br>'; die(); } echo '<table>'; while($row = mysql_fetch_assoc($result)){ var_dump($row); echo '<tr>'; echo '<td>'; echo $row['Tables_in_'.$dbname]; echo '</td>'; echo '<td>'.'<a href = "column.php?tablename='.$row['Tables_in_'.$dbname].'&dbname='.$dbname.'">'; echo '结构'; echo '</a>'.'</td>'; echo '<td>'.'<a href = "data.php?tablename='.$row['Tables_in_'.$dbname].'&dbname='.$dbname.'">'; echo '数据'; echo '</a>'.'</td>'; echo '</tr>'; } echo '</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('Content-Type: text/html;charset=utf-8'); $host = "127.0.0.1"; $port = '3306'; $user = 'root'; $pass = '123654'; $charset = 'utf8'; $link = mysql_connect("$host:$port",$user,$pass); mysql_query('set names '.$charset,$link); $dbname = $_GET['dbname']; $tablename = $_GET['tablename']; $sql = "desc `$dbname`.`$tablename`"; //展示出表的信息 if(!$result = mysql_query($sql,$link)){ echo 'SQL执行失败'.'<br>'; echo "出错的地方是:".$sql.'<br>'; echo "错误代码是:",mysql_errno($this->link).'<br>'; echo "错误信息是:",mysql_error($this->link).'<br>'; die(); } echo '<table>'; while($row = mysql_fetch_assoc($result)){ //var_dump($row); echo '<tr>'; echo '<td>'; echo $row['Field']; //输出名称 echo '</td>'; echo '<td>'; echo $row['Type']; //输出类型 echo '</td>'; echo '</tr>'; } echo '</table>';
<?php header('Content-Type: text/html;charset=utf-8'); $host = "127.0.0.1"; $port = '3306'; $user = 'root'; $pass = '123654'; $charset = 'utf8'; $link = mysql_connect("$host:$port",$user,$pass); mysql_query('set names '.$charset,$link); $dbname = $_GET['dbname'];//获得数据库名 $tablename = $_GET['tablename'];//获得表名 //选择某认数据库 $sql = "use `$dbname`"; if(!$result = mysql_query($sql,$link)){ echo 'SQL执行失败'.'<br>'; echo "出错的地方是:".$sql.'<br>'; echo "错误代码是:",mysql_errno($this->link).'<br>'; echo "错误信息是:",mysql_error($this->link).'<br>'; die(); } //获得字段信息 $sql = "desc `$tablename`"; if(!$result = mysql_query($sql,$link)){ echo 'SQL执行失败'.'<br>'; echo "出错的地方是:".$sql.'<br>'; echo "错误代码是:",mysql_errno($this->link).'<br>'; echo "错误信息是:",mysql_error($this->link).'<br>'; die(); } echo '<table border="1">'; //展示字段 echo "<tr>"; while($rows = mysql_fetch_assoc($result)){ echo "<th>"; echo $rows['Field']; echo "</th>"; } echo "</tr>"; //查询每个表的数据 $sql = "select * from `$tablename` where 1"; if(!$result = mysql_query($sql,$link)){ echo 'SQL执行失败'.'<br>'; echo "出错的地方是:".$sql.'<br>'; echo "错误代码是:",mysql_errno($this->link).'<br>'; echo "错误信息是:",mysql_error($this->link).'<br>'; die(); } //遍历结果集 得到所有字段 while($rows = mysql_fetch_assoc($result)){ echo '<tr>'; foreach ($rows as $value) { echo '<td>'; echo $value === '' ?' ':($value===NULL?'NULL':$value); //三目运算符的嵌套 echo '</td>'; } echo '</tr>'; } echo '</table>';
Related recommendations:
Complex query of MySQL query statement
MySQL query time basic tutorial
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!