1

假设我有一堂课:

class pdoc {
    private static $db_connect_pool;
    public static function openConnect() {
        try {
            $connect_options_arr = array(PDO::ATTR_PERSISTENT => true);

            self::$db_connect_pool[''.DB_DRIVER.'_'.DB_NAME.''] = new PDO("".DB_DRIVER.":host=".DB_HOST.";db_name=".DB_NAME."", DB_USER, DB_PASS, $connect_options_arr);
        } catch (Exception $e) {
            print_r($e);
        }
    }

    public static function getConnection() {
        return self::$db_connect_pool[''.DB_DRIVER.'_'.DB_NAME.''];
    }

    public static function qry($sql) {
        self::openConnect();
        $db_handle = self::getConnection();
        $st_handle = $db_handle->prepare($sql);
        return $st_handle->execute();           
    }
}

然后,调用类:

$sql = "SELECT * FROM sometable";

if(pdoc::qry($sql))  echo "y";
else                 echo "n";

为什么代码总是返回n?我检查了已成功连接的连接,但是当我尝试执行一些查询时,它什么也没返回。有任何想法吗?谢谢。


更新(@Robbie 的代码)

class pdoc {
    private static $db_connect_pool;
    public static function openConnect() {
        try {
            $connect_options_arr = array(PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
            self::$db_connect_pool[''.DB_DRIVER.'_'.DB_NAME.''] = new PDO("".DB_DRIVER.":host=".DB_HOST.";db_name=".DB_NAME."", DB_USER, DB_PASS, $connect_options_arr);
        } catch (Exception $e) {
            print_r($e);
        }
    }

    public static function getConnection() {
        return self::$db_connect_pool[''.DB_DRIVER.'_'.DB_NAME.''];
    }

    public static function qry($sql) {
        self::openConnect();
        $db_handle = self::getConnection();
        try {
            $st_handle = $db_handle->prepare($sql);
            $retval = $st_handle->execute(); //--> Got error on this line       
        } catch (Exception $e) {
            Die('Need to handle this error. $e has all the details');
        }
        return $retval;  
    }
}

错误说:exception 'PDOException' with message 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected'


回答

改变:

... new PDO("".DB_DRIVER.":host=".DB_HOST.";db_name=".DB_NAME."", ...

进入:

... new PDO("".DB_DRIVER.":host=".DB_HOST.";dbname=".DB_NAME."", ...

在捕获错误消息(来自更新的代码)并参考此线程后,我发现该dbname部分已按照dn_name我的代码编写。所以我把它改成dbname它,它工作得很好!感谢@Robbie 提供您的代码!:)

4

2 回答 2

5

如果它什么都不返回,那么它就失败了。您需要调用错误函数 ( errorInfo, errorCode) 来找出原因。

但棘手的部分是你不知道错误是在数据库上还是在语句上,所以最好的技巧是使用异常错误报告,将所有函数包装在一个 try catch 中,异常被捕获时将属于到数据库或语句。更容易处理。

您的代码将是:

class pdoc {
    private static $db_connect_pool;
    public static function openConnect() {
        try {
            $connect_options_arr = array(PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
            self::$db_connect_pool[''.DB_DRIVER.'_'.DB_NAME.''] = new PDO("".DB_DRIVER.":host=".DB_HOST.";db_name=".DB_NAME."", DB_USER, DB_PASS, $connect_options_arr);
        } catch (Exception $e) {
            print_r($e);
        }
    }

    public static function getConnection() {
        return self::$db_connect_pool[''.DB_DRIVER.'_'.DB_NAME.''];
    }

    public static function qry($sql) {
        self::openConnect();
        $db_handle = self::getConnection();
        try {
            $st_handle = $db_handle->prepare($sql);
            $retval = $st_handle->execute();            
        } catch (Exception $e) {
            Die('Need to handle this error. $e has all the details');
        }
        return $retval;  
    }
}
于 2012-11-16T06:05:07.897 回答
3
try in this way.. 

public static function qry($sql) {
try {
    $statement = $db_handle->prepare($sql);
    $retval = $statement->execute();
    if ($statement->rowCount() >= 1) { 
        //do something               
    }else {
        $errors = $statement->errorInfo();
        echo $errors[2] . ", " . $errors[1] . " ," . $errors[0];
    }
} catch (Exception $e) {
    echo $e->getMessage();
    }        
}
于 2012-11-16T06:21:49.757 回答