我在PHP中使用try,但PDO不会捕获异常

时间:2017-04-02 19:29:11

标签: php mysql pdo

嘿,由于某种原因,我会在php中使用try关键字来捕获PDO异常,但它不会。我会随机提供不正确的详细信息,它会显示PDO Uncaught Exception并显示连接详细信息。它的工作原理我可以执行sql语句但是当conn细节不正确时它表示未捕获的PDO异常。这是我正在研究的框架。

全局文件调用所有其他文件,但是只包含数据库类,因为那就是问题所在。

Global.php

<?php
namespace App\Framework;
define('APP_VERSION', '1.0.0');
defined("START") ? null : define("START", microtime());
define('DEBUG', true);
if (DEBUG) {
  error_reporting(E_ALL);
  ini_set('display_errors', 1);
} else {
  error_reporting(0);
  ini_set('display_errors', 0);
}
include_once 'LinkConfig.php';
include_once 'LinkInterface.php';
include_once 'Link.php';
try {
  $Link = new Link(
    LinkConfig::DRIVER,
    LinkConfig::HOST,
    LinkConfig::DBNAME,
    LinkConfig::USER,
    LinkConfig::PASS,
    LinkConfig::CHARSET,
    LinkConfig::PORT
  );
} catch (PDOException $Exception) {
  die('Connection failed: ' . $Exception->getMessage());
}


?>

这是数据库连接接口文件

LinkInterface.php

<?php
namespace App\Framework;
defined('START') || (header('HTTP/1.1 403 Forbidden') & die('403.14 - Directory listing denied.'));
interface ILink {
  public function __construct($Driver, $Host, $DBName, $User, $Pass, $Charset, $Port);
  public function Select($Sql, $Array = array(), $FetchMode = PDO::FETCH_ASSOC);
  public function Insert($Table, array $Data);
  public function Update($Table, $Data, $Where, $WhereBindArray = array());
  public function Delete($Table, $Where, $Bind = array(), $Limit = null);
}

?>

这是数据库连接配置文件。

LinkConfig.php

<?php
namespace App\Framework;
defined('START') || (header('HTTP/1.1 403 Forbidden') & die('403.14 - Directory listing denied.'));
class LinkConfig {
  const DRIVER = 'mysql';
  const HOST = 'localhost';
  const DBNAME = 'test';
  const USER = 'root';
  const PASS = '';
  const CHARSET = 'utf8';
  const PORT = '3306';
  const DEBUG = true;
}

?>

最后一个文件就是这一切。

Link.php

<?php
namespace App\Framework;
defined('START') || (header('HTTP/1.1 403 Forbidden') & die('403.14 - Directory listing denied.'));
use \PDO;
class Link extends PDO {
  public function __construct($Driver, $Host, $DBName, $User, $Pass, $Charset, $Port) {
    parent::__construct($Driver . ':host=' . $Host . ';port=' . $Port . ';dbname=' . $DBName . ';charset=' . $Charset, $User, $Pass);
    $this->exec('SET CHARACTER SET ' . $Charset);
    if (LinkConfig::DEBUG) {
      $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
    }
  }
  public function Select($Sql, $Array = array(), $FetchMode = PDO::FETCH_ASSOC) {
    $Sth = $this->prepare($Sql);
    foreach ($Array as $Key => $Value) {
      $Sth->bindValue(":$Key", $Value);
    }
    $Sth->execute();
    return $Sth->fetchAll($FetchMode);
  }
  public function Insert($Table, array $Data) {
    ksort($Data);
    $FieldNames = implode('`, `', array_keys($Data));
    $FieldValues = ':' . implode(', :', array_keys($Data));
    $Sth = $this->prepare("INSERT INTO $Table (`$FieldNames`) VALUES ($FieldValues)");
    foreach ($Data as $Key => $Value) {
      $Sth->bindValue(":$Key", $Value);
    }
    $Sth->execute();
  }
  public function Update($Table, $Data, $Where, $WhereBindArray = array()) {
    ksort($Data);
    $FieldDetails = null;
    foreach ($data as $Key => $Value) {
      $FieldDetails .= "`$Key`=:$Key,";
    }
    $FieldDetails = rtrim($FieldDetails, ',');
    $Sth = $this->prepare("UPDATE $Table SET $FieldDetails WHERE $Where");
    foreach ($Data as $Key => $Value) {
      $Sth->bindValue(":$Key", $Value);
    }
    foreach ($WhereBindArray as $Key => $Value) {
      $Sth->bindValue(":$Key", $Value);
    }
    $Sth->execute();
  }
  public function Delete($Table, $Where, $Bind = array(), $Limit = null) {
    $Query = "DELETE FROM $Table WHERE $Where";
    if ($Limit) {
      $Query .= " LIMIT $Limit";
    }
    $Sth = $this->prepare($Query);
    foreach ($Bind as $Key => $Value) {
      $Sth->bindValue(":$Key", $Value);
    }
    $Sth->execute();
  }
}

?>

是否有合理的解释为什么它显示未被捕获的PDO异常。

这是我收到的错误消息。

这是它输出Fatal error: Uncaught PDOException: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\vendor\Link.php:8 Stack trace: #0 C:\xampp\htdocs\vendor\Link.php(8): PDO->__construct('mysql:host=loca...', 'root', '') #1 C:\xampp\htdocs\vendor\Global.php(33): App\Framework\Link->__construct('mysql', 'localhostz', 'test', 'root', '', 'utf8', '3306') #2 {main} Next PDOException: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\vendor\Link.php:8 Stack trace: #0 C:\xampp\htdocs\vendor\Link.php(8): PDO->__construct('mysql:host=loca...', 'root', '') #1 C:\xampp\htdocs\vendor\Global.php(33): App\Framework\Link->__construct('mysql', 'localhostz', 'test', 'root', '', 'utf8', '3306') #2 {main} thrown in C:\xampp\htdocs\vendor\Link.php on line 8

的内容

1 个答案:

答案 0 :(得分:0)

所以我认为通过将Global.php更改为:

来使用命名空间
<?php
use App\Framework as Framework;
define('APP_VERSION', '1.0.0');
defined("START") ? null : define("START", microtime());
define('DEBUG', true);
if (DEBUG) {
  error_reporting(E_ALL);
  ini_set('display_errors', 1);
} else {
  error_reporting(0);
  ini_set('display_errors', 0);
}
include_once 'LinkConfig.php';
include_once 'LinkInterface.php';
include_once 'Link.php';
try {
  $Link = new Framework\Link(
    Framework\LinkConfig::DRIVER,
    Framework\LinkConfig::HOST,
    Framework\LinkConfig::DBNAME,
    Framework\LinkConfig::USER,
    Framework\LinkConfig::PASS,
    Framework\LinkConfig::CHARSET,
    Framework\LinkConfig::PORT
  );
} catch (PDOException $Exception) {
  echo 'Connection failed: ' . $Exception->getMessage();
}

?>