天天看點

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語言查詢Mysql資料庫内容

繼續閱讀