2

我正在尝试编写一个小类,以便可以使用它来连接到我的数据库但是我遇到了一个问题,即 PDO 没有显示错误。

我要做的是在查询失败时显示 mysql 错误,以便我知道错误是什么并修复它。

在我的通话中,我有 4 种方法需要捕获 mysql 错误。

startConnection()
getOneResult()
processQuery()
getDataSet()

这是我目前的课程,有人可以告诉我如何显示 mysql 错误。请注意,我尝试使用 try catch 来捕获错误,但这对我不起作用。

谢谢你的帮助

<?php

class connection {

    private $connString;
    private $userName;
    private $passCode;
    private $server;
    private $pdo;
    private $errorMessage;

    private $pdo_opt = array (
                            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
                            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
                            );



    function __construct($dbName, $serverName = 'localhost'){

        //sets credentials
        $this->setConnectionCredentials($dbName, $serverName);

        //start the connect
        $this->startConnection();

    }

    function startConnection(){


            $this->pdo = new PDO($this->connString, $this->userName, $this->passCode, $this->pdo_opt);

            if( ! $this->pdo){

                $this->errorMessage  = 'Failed to connect to database. Please try to refresh this page in 1 minute. ';
                $this->errorMessage .= 'However, if you continue to see this message please contact your system administrator.';
                echo $this->getError();

            }

    }


    //this will close the PDO connection
    public function endConnection(){

        $this->pdo->close;
    }

    //return a dataset with the results
    public function getDataSet($query, $data = NULL)
    {
        $cmd = $this->pdo->prepare( $query );

        $cmd->execute($data);
        return $cmd->fetchAll();
    }


    //return a dataset with the results
    public function processQuery($query, $data = NULL)
    {
        $cmd = $this->pdo->prepare( $query );

        return $cmd->execute($data);
    }


    public function getOneResult($query, $data = NULL){
        $cmd = $this->pdo->prepare( $query );
        $cmd->execute($data);
        return $cmd->fetchColumn();
    }

    public function getError(){
        if($this->errorMessage != '')
            return $this->errorMessage;
        else
            return true;  //no errors found

    }

    //this where you need to set new server credentials with a new case statment
    function setConnectionCredentials($dbName, $serv){

        switch($serv){

            case 'NAME':
                $this->connString   = 'mysql:host='.$serv.';dbname='.$dbName.';charset=utf8';
                $this->userName     = 'user';
                $this->passCode     = 'password';
            break;

            default:
                $this->connString   = 'mysql:host=localhost;dbname=rdi_cms;charset=utf8';
                $this->userName     = 'user';
                $this->passCode     = 'pass!';
            break;

            }

    }



}


?>
4

2 回答 2

2

您不一定要显示错误消息,而只是提出它。显示 mysql 错误消息会使您的站点不那么安全并且看起来很卑鄙。

所以,你唯一关心的就是

  • 使 PDO 对 SQL 错误产生异常。
  • 设置 PHP 以在开发服务器上显示错误消息并登录实时服务器

因此,请在脚本顶部尝试此代码

ini_set('display_errors',1);
error_reporting(E_ALL);

然后使用故意的 mysql 错误运行这些函数之一。您的脚本应该停止并显示错误消息。

更新:
好吧,我尝试运行您的代码,上面的这两条神奇的代码行以其他方式帮助了我:

Notice: Undefined variable: pdo_opt in D:\SERVER\htdocs\0.php on line 37

表示变量拼写错误(您必须$this->pdo_opt改用) - 所以,这就是 PDO 没有引发异常的原因。

这是一个教训,为什么error_reporting应该总是 E_ALL

于 2013-03-25T19:01:37.583 回答
0

我会摆脱或不使用方法getError(),getOneResult()和. 使用该类来管理您的 PDO 连接。然后将您的 PDO 语句包装在 try...catch 中。processQuery()getDataSet()

try
{
    $conn = new connection( "mydatabase" );
    $db = $conn->getPdoInstance();
    $stmt = $db->prepare( "SELECT * FROM `something`" );
    $stmt->execute();
}
catch( PDOException $e )
{
   // Catch the error message and do whatever you need to do with it
}
于 2013-03-25T18:37:23.957 回答