pdo php查询没有显示任何调试

时间:2017-09-22 09:01:11

标签: php sql-server pdo

print_r($con->_con);
echo "(SELECT * FROM fvwItems WHERE itemname LIKE :key)";
echo "\PDO::errorInfo():\n";
print_r($con->_con->errorInfo());// no error showing
$stmt = $con->_con->prepare("(SELECT * FROM fvwItems WHERE itemname LIKE :key)", array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
echo $stmt;//not putting any output
echo "\PDO::errorInfo():\n";
print_r($con->_con->errorInfo());

我有上面的查询,我只得到

Resource id #10
(SELECT * FROM fvwItems WHERE itemname LIKE :key)\PDO::errorInfo():

在chrome的开发工具中。我在try/catch内也有查询,但我没有查询错误或查询中的问题,因为它没有给我足够的信息来判断哪个有问题。

$stmt = $con->_con->prepare("(SELECT * FROM fvwItems WHERE itemname LIKE :key)", array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));以外的任何内容都不会显示错误或不显示

检查查询错误的最佳方法是什么?

UPDATE

我的联系是这样的:

$connectionOptions = array(
            "Database" => DB_NAME,
            "Uid" => DB_USER,
            "PWD" => DB_PASS
        );
        //Establishes the connection
        $this->_connection = sqlsrv_connect($serverName, $connectionOptions);

我以为我需要它new PDO才能运行?

喜欢

$this->_connection = new PDO("sqlsrv:Server=$serverName;Database=".DB_NAME.";",DB_USER,DB_PASS);
print_r($this->_connection);

导致

PDO Object
(
)

但这不是连接,而是收到错误

2 个答案:

答案 0 :(得分:0)

创建PDO对象后设置此选项:

$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

答案 1 :(得分:0)

上面的代码不清楚,但看起来您正在使用sqlsrv_connect()创建一个连接对象,然后尝试将其视为PDO对象。显然这不起作用。

现在,在现实生活中,您将拥有一种更灵活,更有用的捕获错误的方法,但这将向您展示基础知识:

try {
    $opts  = [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION];
    $conn  = new \PDO("sqlsrv:Server=$serverName;Database=" . DB_NAME . ";", DB_USER, DB_PASS, $opts);
    $query = "SELECT * FROM fvwItems WHERE itemname LIKE :key";
    $stmt  = $conn->prepare($query);
    $param = [":key" => $the_keys_value];
    $stmt->execute($param);
    $rows  = $stmt->fetch(\PDO::FETCH_ASSOC);
} catch (\PDOException $e) {
    echo $e->getMessage();
}