关于 php:PDO 执行不返回任何内容

PDO Execute Returns Nothing

假设我有一堂课:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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();          
    }
}

然后,调用类:

1
2
3
4
$sql ="SELECT * FROM sometable";

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

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

更新(@Robbie 的代码)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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'.

回答

变化:

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

进入:

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

在捕获错误消息(来自更新的代码)并参考此线程后,我发现 dbname 部分在我的代码中写为 dn_name 。因此,我将其更改为 dbname 并且效果很好!感谢@Robbie 提供您的代码! :)


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

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

你的代码是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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();
    }        
}