• mysqli类实例


    <?php
    
    class Mysql {
        private $mysqli;
        private static $ins;
    
        private function __construct(){
            $this->mysqli = new mysqli('192.168.88.10','root','root','blog');
            if(mysqli_connect_errno()) {
                printf('%s',mysqli_connect_error());
                $this->mysqli = false;
                exit;
            }
            $this->mysqli->set_charset('gbk');
        }
    
        public static function getMysql() {
            if(!(self::$ins instanceOf self)) {
                self::$ins = new self();
            }
            return self::$ins;
        }
    
        public function mQuery($sql) {
            return $this->mysqli->query($sql);
        }
    
        public function mGetAll($sql) {
            $rs = $this->mQuery($sql);
            if(!$rs) {
                return false;
            }
            $data = array();
            while($row = $rs->fetch_assoc()) {
                $data[] = $row;
            }
            $rs->close();
            return $data;
        }
    
        public function mGetRow($sql) {
            $rs = $this->mQuery($sql);
            if(!$rs) {
                return false;
            }
            $row = $rs->fetch_assoc();
            $rs->close();
            return $row;
        }
    
        public function mGetOne($sql) {
            $rs = $this->mQuery($sql);
            if(!$rs) {
                return false;
            }
            $row = $rs->fetch_row();
            $rs->close();
            return $row[0];
        }
        
        /*
            insert into cat(catname,num) values('aa',0);
            update cat set catname='aa',num=900 where cat_id = 10;
        */
        public function mExec($table,$data,$act='insert',$where='0') {
            
            $sql = '';
            if($act == 'insert') {
                $sql .= 'insert into '.$table .'(';
                $sql .= implode(',',array_keys($data)).') ';
                $sql .= ' values("';
                $sql .= implode('","',array_values($data)).'")';
            }elseif($act == 'update') {
                $sql .= 'update '.$table.' set ';
                foreach($data as $k=>$v) {
                    $sql .= $k.'="'.$v.'",';
                }
                $sql = rtrim($sql,',');
                $sql .= ' where '.$where;
            }
    
            return $this->mQuery($sql);
        }
    
        public function close(){
            if($this->mysqli) {
                $this->mysqli->close();
            }
        }
    
        public function __destruct() {
            $this->close();
        }
    }
    
    /*
    header('Content-type:text/html;charset=gbk');
    $db = Mysql::getMysql();
    //$sql = 'select count(*) from cat where cat_id = 1';
    //var_dump($db->mGetOne($sql));
    $data = array('catname'=>'testaa','num'=>100);
    
    $db->mExec('cat',$data,'update','cat_id=1');
    
    */
    

      

  • 相关阅读:
    vpp l3 bvi
    set interface ip address: failed to add 1 on loop1 which conflicts with
    Failed: no source address for egress interface
    vpp vrf
    vpp bvi
    creates 2 connected namespaces vpp1 & vpp2
    unknown input `arp'
    vpp cmd
    vxlan bum
    Go流程结构(if)
  • 原文地址:https://www.cnblogs.com/yc12/p/11334602.html
Copyright © 2020-2023  润新知