- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嘿,出于某种原因,我将在 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/
我刚刚遇到了一个非常奇怪的行为。这是代码: // So far everything's fine val x: Try[Try[Unit]] = Try(Try{}) x: scala.util.T
“输出”是一个序列化的 OpenStruct。 定义标题 try(:output).try(:data).try(:title) 结束 什么会更好? :) 最佳答案 或者只是这样: def title
我有以下元组 - (t1,t2) :(Try,Try) 我想检查两者是否成功或其中之一是否失败,但避免代码重复。像这样的东西: (t1,t2) match { case (Success(v1),Su
是否必须放置内部 try-with-resources 或其中一个 try-with-resources 中的所有内容都会自动关闭? try (BasicDataSource ds = Bas
有一点特殊,尝试创建一段 try catch 代码来处理 GoogleTokenResponse,但编译器在 try 时抛出异常错误。有什么想法吗? 错误信息: | Loading Grails 2.
它几乎可以在所有语言中找到,而且我大部分时间都在使用它。 我不知道它是内部的,不知道它是如何真正起作用的。 它如何在任何语言的运行时在 native 级别工作? 例如:如果在 try 内部发生 sta
为什么在 readFile2() 中我需要捕获 FileNotFoundException 以及稍后由 close( ) 方法,并且在 try-with-resources(inside readfi
我正在使用 Apache POI 尝试读取 Word 文件,但即使您使用过 Apache POI,这仍然应该是可以回答的。在 HWPF.extractor 包中有两个对象:WordExtractor
如果try-catch的catch block 中抛出异常,那么finally block 会被调用吗? try { //some thing which throws error } cat
这个问题已经有答案了: What's the purpose of try-with-resources statements? (7 个回答) 已关闭 3 年前。 我一直在查看代码,并且已经看到了对
这个问题已经有答案了: What's the purpose of try-with-resources statements? (7 个回答) 已关闭 3 年前。 我一直在查看代码,并且已经看到了对
我正在使用 Try::Tiny尝试捕捉。 代码如下: use Try::Tiny; try { print "In try"; wrongsubroutine(); # undefi
我想知道这样的代码是否会在抛出异常后总是中断而不继续运行,因此代码不会继续执行第二个 temp.dodaj(b)。 Avto *a = new Avto("lambo",4); Avt
我知道在try子句中必须有一个与资源关联的变量声明。 但是除了被分配一个通常的资源实例化之外,它是否可以被分配一个已经存在的资源,例如: public String getAsString(HttpS
我有一个写的方法。此方法仅扫描用户输入的整数输入。如果用户输入一个字符值,它将抛出一个输入不匹配异常,这是在我的 Try-Catch 语句中处理的。问题是,如果用户输入任何不是数字的东西,然后抛出异常
我注意到这不会编译: PrintWriter printWriter = new PrintWriter("test.txt"); printWriter.append('a'); printWrit
我经常看到人们写这样的代码: try: some_function() except: print 'something' 当我认为这样做更干净时: try: some_functio
该应用程序将在第二个显示器上正常显示内容。问题是当我旋转 iPad 时内容不会在 iPad 上旋转。 看过: http://developer.apple.com/library/ios/#qa/qa
我正在学习 java,我发现我不喜欢的一件事通常是当我有这样的代码时: import java.util.*; import java.io.*; public class GraphProblem
我使用 C++ 有一段时间了,对普通的 try/catch 很熟悉。但是,我现在发现自己在 Windows 上,在 VisualStudio 中编码以进行 COM 开发。代码的几个部分使用了如下内容:
我是一名优秀的程序员,十分优秀!