好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

php语言如何查询Mysql数据库内容

php语言查询Mysql数据库内容的方法:首先php页面在进行浏览时需要有php语言执行的环境;然后建立php类文件【mysql.php】进行具体的操作;接着建立页面文件【index.php】进行接收数据;最后访问路径即可。

【 相关学习推荐: mysql教程 】

php语言查询Mysql数据库内容的方法:

1.php页面在进行浏览时需要有php语言执行的环境,本人用的是WampServer软件,只要将项目复制到 wampserver_php\wamp\www\ 该路径下就可以执行php语言。

2.建立php类文件(mysql.php)进行具体的操作

<?php
/*设置内容类型和编码样式*/
header("content-type:text/html;charset=utf-8");
/*对数据库操作*/
class dbMysqli{
    private $conn = null;
    public $message = "";
    /*设置错误接受机制*/
    function Message($mes,$flag=true){
   if($flag){
  $this->message .="<div style='color:green;font-size:12px;'>".$mes."</div>";
   }else{
  $this->message .="<div style='color:green;font-size:12px;'>".$mes."</div>";
   }
    }
    
    /*连接数据库服务器,设置连接数据库编码*/
    function __construct($host,$user,$pwd,$dbName,$charset){
   //连接数据库服务器选择数据库
   $this->conn = new mysqli($host,$user,$pwd,$dbName);
   if($this->conn === false){
  $this->Message("连接数据库失败",false);
  return false;
   }else{
  $this->Message("连接数据库成功,选择数据库");
   }
   //设置连接编码
   if($this->conn->set_charset($charset)){
  $this->Message("设置编码成功");
   }else{
  $this->Message("设置编码失败",false);
   }
    }
    /*查询数据库数据*/
    public function MoreData($sql){
   $sql = trim($sql);
   /*检查sql语言是否正确*/
   $result = preg_match('/^select/i',$sql);
   if($result){
  //执行sql语句
  $rs = $this->conn->query($sql);
  if($rs === false){
 $this->Message("执行'".$sql."'失败,失败原因:".$this->conn->error,false);
 return false;
  }else{
 $this->Message("执行'".$sql."'成功");
 $RS = $rs->fetch_all(MYSQL_ASSOC);
 $rs->free();
 return $RS;
  }
   }else{
  $this->Message("执行'".$sql."'失败",false);
  return false;
   }
    }
}
/*链接数据库地址、用户名,密码,数据库名,编码方式*/
$db = new dbMysqli('localhost','root','cly8','user','utf-8');

3.建立页面文件(index.php)进行接收数据

<?php 
    header("content-type:text/html;charset=utf-8");
    error_reporting(E_ALL);
    //引入一个数据库操作类
    include_once 'mysql.php';
    //查询数据
    $rs = $db->MoreData("select * from student");
?>
<html>
    <head>
   <meta charset="utf-8" />
   <title>css3实现多彩动态漩涡线条特效动画</title>
    </head>
    <style>
    table{
    font-family: verdana,arial,sans-serif;
    font-size:11px;
    color:#333333;
    border-width: 1px;
    border-color: #666666;
    border-collapse: collapse;
    }
    table th {
    border-width: 1px;
    padding: 8px;
    border-style: solid;
    border-color: #666666;
    background-color: #dedede;
    }
    table td {
    border-width: 1px;
    padding: 8px;
    border-style: solid;
    border-color: #666666;
    background-color: #ffffff;
    }
    </style>
<body>
    <table>
   <tr>
  <th>编号</th>
  <th>姓名</th>
  <th>密码</th>
   </tr>
   <?php foreach($rs as $val) {?>
   <tr>
  <td><?php echo $val['Sid'];?></td>
  <td><?php echo $val['Sname'];?></td>
  <td><?php echo $val['Password'];?></td>
   </tr>
   <?php }?>
    </table>
</body>
</html>

4.最后访问路径http://localhost/文件夹名/index.php

想了解更多相关学习,敬请关注php培训栏目!

以上就是php语言如何查询Mysql数据库内容的详细内容!

查看更多关于php语言如何查询Mysql数据库内容的详细内容...

  阅读:49次