2

データベースに接続するために使用できるように小さなクラスを作成しようとしていますが、PDOがエラーを表示しないという問題があります。

私がやろうとしているのは、クエリが失敗したときにmysqlエラーを表示することです。これにより、エラーが何であるかがわかり、修正できます。

私の呼び出しでは、mysqlエラーをキャッチする必要がある4つのメソッドがあります。

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エラーメッセージを表示すると、サイトの安全性が低下し、意味のあるものに見えます。

だから、あなたの唯一の懸念は

  • SQLエラーでPDOtheow例外を作成します。
  • エラーメッセージを開発サーバーに表示し、ライブサーバーにログオンするようにPHPを設定する

したがって、スクリプトの上部でこのコードを試してください

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

次に、意図的なmysqlエラーでこれらの関数の1つを実行します。スクリプトを停止して、エラーメッセージを表示する必要があります。

更新:
まあ、私はあなたのコードを実行しようとしました、そして上からのこれらの2つの魔法のコード行は私を他の方法で助けました:

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 に答える