PDO执行返回任何内容

时间:2012-11-16 06:01:45

标签: php pdo

假设我有一个班级:

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'


ANSWER

变化:

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

成:

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

在收到错误消息(来自更新后的代码)并引用this thread后,我发现dbname部分已在我的代码中写为dn_name。所以我把它改成dbname,它完美无缺!感谢@Robbie为您的代码! :)

2 个答案:

答案 0 :(得分:4)

如果没有返回任何内容,则失败。您需要调用错误函数(errorInfoerrorCode)以找出原因。

但棘手的部分是你不知道错误是在数据库还是语句上,所以最好的办法就是使用异常错误报告,包装try catch中的所有函数,以及异常,当被困时,将属于DB或语句。更容易处理。

您的代码将是:

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;  
    }
}

答案 1 :(得分:2)

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();
    }        
}