php 之mysqli简单封装

系统 1843 0

1:DBHelper.class.php

      <?
      
        php

    
      
      
        class
      
      
         DBHelper{

    
      
      
        private
      
      
        $mysqli
      
      
        ;

    
      
      
        private
      
      
        static
      
      
        $host
      
      ='127.0.0.1'
      
        ;

    
      
      
        private
      
      
        static
      
      
        $user
      
      ='root'
      
        ;

    
      
      
        private
      
      
        static
      
      
        $pwd
      
      ='mysql'
      
        ;

    
      
      
        private
      
      
        static
      
      
        $dbname
      
      ='test'
      
        ;

    

    
      
      
        //
      
      
        通过构造方法进行初始化操作
      
      
        public
      
      
        function
      
      
         __construct(){

        
      
      
        $this
      
      ->mysqli=
      
        new
      
       mysqli(self::
      
        $host
      
      ,self::
      
        $user
      
      ,self::
      
        $pwd
      
      ,self::
      
        $dbname
      
      
        )

        or 
      
      
        die
      
      ('数据库链接出错:'.
      
        $this
      
      ->mysqli->
      
        connect_error);    

        
      
      
        //
      
      
        设置数据库编码为utf8
      
      
        $this
      
      ->mysqli->query('set names utf8'
      
        );    

    }    



    
      
      
        //
      
      
        执行查询语句
      
      
        public
      
      
        function
      
       execute_dml(
      
        $sql
      
      
        ){

        
      
      
        $arr
      
      =
      
        array
      
      
        ();

        
      
      
        $result
      
      =
      
        $this
      
      ->mysqli->query(
      
        $sql
      
      ) or 
      
        die
      
      (
      
        $this
      
      ->mysqli->
      
        error);

        
      
      
        if
      
      (
      
        $result
      
      
        ){

        
      
      
        while
      
      (
      
        $row
      
      =
      
        $result
      
      ->
      
        fetch_assoc()){

            
      
      
        //
      
      
        将查询结果封装到一个数组中,返回给方法调用处
      
      
        $arr
      
      []=
      
        $row
      
      
        ;

        }    

        
      
      
        //
      
      
        释放查询结果资源
      
      
        $result
      
      ->
      
        free();

        }    

        
      
      
        return
      
      
        $arr
      
      
        ;

    }

    

    
      
      
        //
      
      
        执行增加、删除、更新语句
      
      
        public
      
      
        function
      
       execute_dql(
      
        $sql
      
      
        ){

        
      
      
        $result
      
      =
      
        $this
      
      ->mysqli->query(
      
        $sql
      
      ) or 
      
        die
      
      (
      
        $this
      
      ->mysqli->
      
        error);

        
      
      
        if
      
      (!
      
        $result
      
      
        ){

        
      
      
        return
      
       0;
      
        //
      
      
        表示操作失败    
      
      

        }
      
        else
      
      
        {

        
      
      
        if
      
      (
      
        $this
      
      ->mysqli->affected_rows>0
      
        ){

            
      
      
        return
      
       1;
      
        //
      
      
        操作成功    
      
      

        }
      
        else
      
      
        {

            
      
      
        return
      
       2;
      
        //
      
      
        没有受影响的行    
      
      
                }

        }

    }

    }


      
      ?>
    

 2:使用案例index.php

      <!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title></title>

</head>

<body>

<?
      
        php

    
      
      
        
          require_once
        
        ('DBHelper.class.php'
        
          ); 
        
        
          $dbhelper
        
        =
        
          new
        
        
           DBHelper(); 
        
        
          $sql
        
        ='select id,name,age from user'
        
          ; 
        
        
          $users
        
        =
        
          $dbhelper
        
        ->execute_dml(
        
          $sql
        
        
          ); 
        
      
      
        if
      
      (!
      
        empty
      
      (
      
        $users
      
      
        )){


      
      ?>

<table style="width:80%;">

    <tr>

    <th>ID</th>

    <th>Name</th>

    <th>Age</th>

    <th>操作</th>

    </tr>

    <?
      
        php

    
      
      
        foreach
      
      (
      
        $users
      
      
        as
      
      
        $user
      
      
        ){

    
      
      ?>

    <tr align='center'>

    <td><?php 
      
        echo
      
      
        $user
      
      ['id'];?></td>

    <td><?php 
      
        echo
      
      
        $user
      
      ['name'];?></td>

    <td><?php 
      
        echo
      
      
        $user
      
      ['age'];?></td>

    <td>

        <a href="delete.php?id=<?php echo 
      
        $user
      
      ['id'];?>">Delete</a> &nbsp;|&
      
        nbsp;

        
      
      <a href="show.php?id=<?php echo 
      
        $user
      
      ['id'];?>">Show</a>    

    </td>

    </tr>

    <?php }?>

</table>

<?
      
        php 

    }
      
      
        else
      
      
        {

    
      
      
        echo
      
       '<h1>No result!</h1>'
      
        ;

    }


      
      ?>

<hr/>

<a href="add.php" style="font-size:24px;font-weight:bold;">Add a 
      
        new
      
       user</a>

</body>

</html>
    

 

php 之mysqli简单封装


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论