登录
首页 >  数据库 >  MySQL

pdo 操作类

来源:SegmentFault

时间:2023-01-24 09:29:50 400浏览 收藏

本篇文章向大家介绍《pdo 操作类》,主要包括MySQL,具有一定的参考价值,需要的朋友可以参考一下。

1.代码示例

dsn = 'mysql:host='.$dbHost.';dbname='.$dbName;
            $this->dbn = new PDO($this->dsn, $dbUser, $dbPasswd);
            $this->dbn->exec('SET character_set_connection='.$charSet.', character_set_results='.$charSet.'character_set_client=binary');
        }catch(PDOException $e){
           $this->outputError($e->getMessage());
        }
    }

    /**
     * 输出错误信息
     * @author mmy
     */
    private  function outputError($errorMessage)
    {
        throw new Exception('MySql Error:'.$errorMessage);
    }

    /**
     * 单例模式
     * @author mmy
     * @ return obj
     */
    public static function getInstance($dbHost,$dbUser,$dbPasswd,$dbName,$charSet)
    {
        if(self::$_instance===null)
        {
            self::$_instance = new self($dbHost,$dbUser,$dbPasswd,$dbName,$charSet);
        }
        return self::$_instance;
    }

    /**
     * 防止克隆
     * @author mmy
     */
    private function __clone(){}

    /**
     * 调试方法
     * @author mmy
     */
    private function debug($debugInfo)
    {
        var_dump($debugInfo);
        exit();
    }

    /**
     * 执行sql语句
     * @author mmy
     *
     */
    public function execSql($sql, $debug=false)
    {
        if($debug===true)
        {
            $this->debug($sql);
        }
        $result = $this->dbn->exec($sql);
        $this->getPDOError();
        return $result;
    }

    /**
     * 捕获pdo错误消息
     * @author mmy
     */
    private function getPDOError()
    {
        if($this->dbn->errorCode() !='00000')
        {
            $arrayError = $this->dbn->errorInfo();
            $this->outputError($arrayError[2]);
        }
    }
}

循环引用目录当中的文件

// 加载所有Applications/*/start.php,以便启动所有服务
foreach(glob(__DIR__.'/Applications/*/start*.php') as $start_file)
{
    require_once $start_file;
}

今天带大家了解了MySQL的相关知识,希望对你有所帮助;关于数据库的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表