本文主要和大家分享PHP如何实现MYSQL查询功能,本文主要以代码的形式和大家分享,希望能帮助到大家。
相关mysql视频教程推荐:《mysql教程》
展示数据库<?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执行失败'.'
';
echo "出错的地方是:".$sql.'
';
echo "错误代码是:",mysql_errno($this->link).'
';
echo "错误信息是:",mysql_error($this->link).'
';
die();
}
echo '
//mysql_fetch_assoc() 从结果集中取得一行作为关联数组
while($row = mysql_fetch_assoc($result)){
// var_dump($row);
echo '
';echo '
'.' ';echo $row['Database']; //输出数据库名
'.'';echo '
';}
echo '
';执行完sql语句之后返回的就是结果集;
实现结果如下:
展示表<?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执行失败'.'
';
echo "出错的地方是:".$sql.'
';
echo "错误代码是:",mysql_errno($this->link).'
';
echo "错误信息是:",mysql_error($this->link).'
';
die();
}
echo '
while($row = mysql_fetch_assoc($result)){
var_dump($row);
echo '
';echo '
';echo $row['Tables_in_'.$dbname];
echo '
';echo '
'.'$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执行失败'.'
';
echo "出错的地方是:".$sql.'
';
echo "错误代码是:",mysql_errno($this->link).'
';
echo "错误信息是:",mysql_error($this->link).'
';
die();
}
echo '
while($row = mysql_fetch_assoc($result)){
//var_dump($row);
echo '
';echo '
';echo $row['Field']; //输出名称
echo '
';echo '
';echo $row['Type']; //输出类型
echo '
';echo '
';}
echo '
';表中数据的展示<?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执行失败'.'
';
echo "出错的地方是:".$sql.'
';
echo "错误代码是:",mysql_errno($this->link).'
';
echo "错误信息是:",mysql_error($this->link).'
';
die();
}
//获得字段信息
$sql = "desc `$tablename`";
if(!$result = mysql_query($sql,$link)){
echo 'SQL执行失败'.'
';
echo "出错的地方是:".$sql.'
';
echo "错误代码是:",mysql_errno($this->link).'
';
echo "错误信息是:",mysql_error($this->link).'
';
die();
}
echo '
//展示字段
echo "
";while($rows = mysql_fetch_assoc($result)){
echo "
";echo $rows['Field'];
echo "
";}
echo "
";//查询每个表的数据
$sql = "select * from `$tablename` where 1";
if(!$result = mysql_query($sql,$link)){
echo 'SQL执行失败'.'
';
echo "出错的地方是:".$sql.'
';
echo "错误代码是:",mysql_errno($this->link).'
';
echo "错误信息是:",mysql_error($this->link).'
';
die();
}
//遍历结果集 得到所有字段
while($rows = mysql_fetch_assoc($result)){
echo '
';foreach ($rows as $value) {
echo '
';echo $value === '' ?' ':($value===NULL?'NULL':$value);
//三目运算符的嵌套
echo '
';}
echo '
';}
echo '
';相关推荐: