zoukankan      html  css  js  c++  java
  • php语言查询Mysql数据库内容

    通过php语言实现对Mysql数据库的基本操作

    1.php页面在进行浏览时需要有php语言执行的环境,本人用的是WampServer软件,只要将项目复制到wampserver_phpwampwww该路径下就可以执行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- 1px;
        border-color: #666666;
        border-collapse: collapse;
        }
        table th {
        border- 1px;
        padding: 8px;
        border-style: solid;
        border-color: #666666;
        background-color: #dedede;
        }
        table td {
        border- 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

  • 相关阅读:
    hihoCoder 1392 War Chess 【模拟】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)
    HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)
    Codeforces 715B & 716D Complete The Graph 【最短路】 (Codeforces Round #372 (Div. 2))
    Codeforces 715A & 716C Plus and Square Root【数学规律】 (Codeforces Round #372 (Div. 2))
    Codeforces 716A Crazy Computer 【模拟】 (Codeforces Round #372 (Div. 2))
    Codeforces 716B Complete the Word【模拟】 (Codeforces Round #372 (Div. 2))
    HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)
    axios 简介与安装
    serializer 功能
    APIview 整理
  • 原文地址:https://www.cnblogs.com/minong/p/9634241.html
Copyright © 2011-2022 走看看