gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:09:50 27 4
gpt4 key购买 nike

嘿,出于某种原因,我将在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com