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

标签 php mysql pdo

嘿,出于某种原因,我将在 php 中使用 try 关键字来捕获 PDO 异常,但它不会。我会随机输入不正确的详细信息,它会显示 PDO 未捕获异常并显示连接详细信息。它有效,我可以执行 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());
}


?>

这是DB连接接口(interface)文件

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 :未捕获的PDOException:PDO::__construct():php_network_getaddresses:getaddrinfo失败:没有这样的主机已知。在 C:\xampp\htdocs\vendor\Link.php:8 堆栈跟踪: #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 失败: 没有这样的主机已知。在 C:\xampp\htdocs\vendor\Link.php:8 堆栈跟踪: #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} 在第 8 行 C:\xampp\htdocs\vendor\Link.php 中抛出

最佳答案

所以我发现这是通过将 Global.php 更改为 namespace 的使用:

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

?>

关于php - 我在 PHP 中使用 try 但 PDO 不会捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43172626/

相关文章:

mysql - 查询出了什么问题?

php - “pdo_mysql” 已禁用,我无法启用它。我在 iMac 7.1 OSX 10.6.8 上安装了 MAMP v. 3.0.4

php - 将 SQL 函数作为 PDO 准备语句运行并返回值

mysql - vb.net DateTimePicker 删除时间 12 :00:00 AM

javascript - 在 Sequelize 的 "where"子句中使用联结表中的列?

php - PDO 选择查询的安全性?

PHP 代码看起来不错,但没有更新

php - 如何插入auto_increment

php - 我的 php 脚本将空白信息输入到 mysql 数据库中!

php - 本地 WordPress 开发问题 index.php 和管理工作 - 没有其他页面响应