mysql 操作类

本文介绍了一个PHP类,用于简化数据库操作,包括连接、查询、错误处理等功能,并提供了多种查询结果处理方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php
class SqlModel
{
    private $t;
    public $Real;
    private $row;
    public $WrSql;
    private $Name;
    private $List;
    public $conf=array(
        'HostName'=>'127.0.0.1',
        'UserName'=>'root',
        'Password'=>'root',
        'DataBase'=>'database',
        'Port'    =>'3306'
    );

    /**
     * @Type        1:写权限, 2:读权限
     */
    function __construct($Type = 2)
    {

        // 假如报错则
        try
        {
            // 连接数据库
            $this->con = new mysqli($this->conf['HostName'], $this->conf['UserName'], $this->conf['Password'], $this->conf['DataBase'], $this->conf['Port']);
            // 设置链接编码
            $this->con->query("set names 'utf8'");
            // 判断是否有错误
            if ((int)$this->con->{'connect_errno'} != 0)
            {
                $this->Error();
            }
        }
        catch (Exception $e)
        {
            $this->Error();
        }
    }
    

    /**
     * @Query       数据查询处理
     */
    private function Query()
    {
        try
        {
            if($this->box)
            {
                $this->blackbox = array();
                // 循环保护数据
                foreach($this->box as $List => $Name)
                {
                    $this->blackbox["<{[$List]}>"] = $this->con->real_escape_string($Name);
                }
                // 批量替换字符串
                $this->sql = strtr($this->sql, $this->blackbox);
SaveLog("MySQL", $this->sql);
            }
            $this->query = $this->con->query($this->sql);
        }
        catch (Exception $t)
        {
            $this->Error();
        }
    }

    /**
     * @Error       查询错误处理
     */
    private function Error ()
    {
        // 判断是否有查询语句
        if (isset($this->sql))
        {
         echo $this->sql;
            // 查询出错关闭
            $this->con->close();
        }
        else
        {
            echo "数据库无法连接";
        }
        // 判断是否有查询语句
        if (isset($this->sql))
        {
            SaveLog("SqlError", json_encode([time(), $this->sql]));
        }
        else
        {
            SaveLog("SqlError", $this->conf['HostName']."数据库无法连接");
/*
            // 发送短信通知
            $SMSReturn = new MessageModel('SMS_84655019');
            $MaileError = $SMSReturn->send_verify(Yaconf::get("TaskYun.AdminPhone"), array(
                "name"      => $this->conf['HostName'],
            ));
*/
        }
        exit;
    }

    /**
     * @Close       查询结束
     */
    public function Close ()
    {
        $this->con->close();
    }

    /**
     * @WrArrayRow          获取多行信息,包含字段名和字段排序
     * =======================================================
     * @WrSql               执行的SQL语句
     * @Real                防御字符数组
     */
    public function WrArrayRow ($WrSql, $Real = NULL)
    {
        // 假如报错则
        try
        {
            // 初始化
            $this->data = array();
            $this->sql = $WrSql;
            $this->box = $Real;
            // 执行数据库查询
            $this->Query();
            // 循环提取查询数据
            while($row = $this->query->fetch_assoc())
            {
                $this->data[] = $row;
            }
            // 清理缓存
            $this->query->free();
            return $this->data;
        }
        catch(Error $t)
        {
            $this->Error();
        }
    }

    /**
     * @WrArrayOneAssoc     提取所有数据行的第一个值
     */
    public function WrArrayOneAssoc($WrSql, $Real = NULL)
    {
        // 假如报错则
        try
        {
            // 初始化
            $this->data = array();
            $this->sql = $WrSql;
            $this->box = $Real;
            // 执行数据库查询
            $this->Query();
            
            while($row = $this->query->fetch_assoc())
            {
                $this->data[] = current($row);
            }
            $this->query->free();
            return $this->data;
        }
        catch(Error $t)
        {
            $this->Error();
        }
    }

    /**
     * @WrArrayTwoAssoc     提取所有数据行的第一个值为键最后一个值为行
     */
    public function WrArrayTwoAssoc($WrSql, $Real = NULL)
    {
        // 假如报错则
        try
        {
            // 初始化
            $this->data = array();
            $this->sql = $WrSql;
            $this->box = $Real;
            // 执行数据库查询
            $this->Query();
            // 提取所有返回数据行
            while($row = $this->query->fetch_assoc())
            {
                $this->data[reset($row)] = end($row);
            }
            $this->query->free();
            return $this->data;
        }
        catch(Error $t)
        {
            $this->Error();
        }
    }

    /**
     * @WrOneAssoc          只提取一行结果
     */
    public function WrOneAssoc($WrSql, $Real = NULL)
    {
        // 假如报错则
        try
        {
            // 初始化
            $this->sql = $WrSql;
            $this->box = $Real;
            // 执行数据库查询
            $this->Query();
            // 只提取一行返回数据行
            $this->data = $this->query->fetch_assoc();
            $this->query->free();
            return $this->data;
        }
        catch(Error $t)
        {
            $this->Error();
        }
    }

    /**
     * @WrAssoc             只提取第一行第一个数据
     */
    public function WrAssoc($WrSql, $Real = NULL)
    {
        // 假如报错则
        try
        {
            // 初始化
            $this->sql = $WrSql;
            $this->box = $Real;
            // 执行数据库查询
            $this->Query();
            // 只提取一行返回数据行
            $this->data = $this->query->fetch_assoc();
            $this->query->free();
            // 判断是否数组
            if(is_array($this->data))
            {
                return current($this->data);
            }
            return null;
        }
        catch(Error $t)
        {
            $this->Error();
        }
    }

    /**
     * @WrRrows             提取受影响行数
     */
    public function WrRrows($WrSql, $Real = NULL)
    {
        // 假如报错则
        try
        {
            // 初始化
            $this->sql = $WrSql;
            $this->box = $Real;
            // 执行数据库查询
            $this->Query();
            // 判断是否有影响行数
            if(isset($this->query->{'num_rows'}))
            {
                $this->data = $this->query->{'num_rows'};
            }
            else
            {
                switch($this->query)
                {
                    case true:$this->data = 1;break;
                    case false:$this->data = 0;break;
                    case NULL:$this->data = 0;break;
                    default:
                        $this->data = $this->query->{'num_rows'};
                    break;
                }
            }
            return $this->data;
        }
        catch(Error $t)
        {
            $this->Error();
        }
    }

    /**
     * @WrOneKey            返回查询数组的键为第一个数据值为之后的所有数据
     */
    public function WrOneKey($WrSql, $Real = NULL)
    {
        // 假如报错则
        try
        {
            // 初始化
            $this->data = array();
            $this->sql = $WrSql;
            $this->box = $Real;
            // 执行数据库查询
            $this->Query();
            // 轮询所有行数据
            while($row = $this->query->fetch_assoc())
            {
                // 提取第一个值数据为键,并保存数据
                $this->data[reset($row)] = $row;
            }
            $this->query->free();
            return $this->data;
        }
        catch(Error $t)
        {
            $this->Error();
        }
    }

    /**
     * @WrInsertId              获取INSERT产生的ID
     */
    public function WrInsertId($WrSql, $Real = NULL)
    {
        // 假如报错则
        try
        {
            // 初始化
            $this->sql = $WrSql;
            $this->box = $Real;
            // 执行数据库查询
            $this->Query();
            // 查询INSERT产生的ID
            $this->data = mysqli_insert_id($this->con);
            // 返回数据
            return $this->data;
        }
        catch(Error $t)
        {
            $this->Error();
        }
    }
}
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值