gpt4 book ai didi

php - 无法在 PHP 中使用 PDO 获取错误信息

转载 作者:行者123 更新时间:2023-11-30 22:27:06 24 4
gpt4 key购买 nike

我正在学习 PDO 并尝试在我当前的项目中实现它。当我使用 mysqli 时,我可以使用 mysqli_error($connection) 获取有关任何错误的详细信息。我用谷歌搜索了 PDO 的可比性是什么,然后找到了 this发布,并决定在我的代码中实现它。但是,即使我知道 sql 语句中存在明显错误,我也无法收到任何错误消息。

相关代码:

class Database {

public $connection;

function __construct() {
$this->open_database_connection();
}

public function open_database_connection() {


try {

$this->connection = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD);

$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->connection->setAttribute( PDO::ATTR_EMULATE_PREPARES, false);


} catch(PDOException $e) {
echo $e->getMessage();
die('Could not connect');
}

} // end of open_database_connection method




public function query($sql, $params = []) {

try {

$query = $this->connection->prepare($sql);

} catch (Exception $e) {
var_dump('mysql error: ' . $e->getMessage());
}


foreach($params as $key => $value) {
if ($key === ':limit') {
$query->bindValue($key, $value, PDO::PARAM_INT);

} else {
$query -> bindValue($key, $value);
}
}

try {

$query->execute();

} catch(Exception $e) {
echo 'Exception -> ';
var_dump($e->getMessage());
}


/*
DID NOT WORK:
if (!$query->execute()) {
print_r($query->errorinfo());

}*/

$result = $query->fetchAll(PDO::FETCH_ASSOC);

$this->confirm_query($result);
return $result;

} // end of query method



function confirm_query($query) {

if (!$query) {
die('mysql error: ');
}

当我运行代码时,我确实收到了“mysql 错误”,但我没有得到有关它的任何详细信息。我做错了什么?

更新:根据要求,我在下面提供更多详细信息。

我想做的是验证用户的登录详细信息。为此,一旦用户输入了他的凭证,这段代码就会运行:

if (isset($_POST['submit'])) {

$username = trim($_POST['username']);
$password = trim($_POST['password']);

//check the database for the user

$user_found = User::verify_user($username, $password);

User类的相关代码:

    public static function verify_user($username, $password) {

global $db;

$username = $db->escape_string($username);
$password = $db->escape_string($password);

$values = [ ":username" => $username,
":password" => $password,
":limit" => 1
];


$result_array = self::find_this_query("SELECT * FROM users WHERE username=:username AND password=:password LIMIT :limit", true, $values);


return !empty($result_array)? array_shift($result_array) : false;

}




public static function find_this_query($sql, $prepared = false, $params = []) {

global $db;

$the_object_array = array();
$result = $db->query($sql, $params);

$arr_length = count($result);

for ($i = 0; $i < $arr_length; $i++) {

$the_object_array[] = self::instantiation($result[$i]);
}

return $the_object_array;

}


public static function instantiation($the_record) {

$the_object =new self; //we need new self because $the_record corresponds to one user!



foreach($the_record as $the_attribute => $value) {

if ($the_object->has_the_attribute($the_attribute)) {
$the_object->$the_attribute = $value;
}
}


return $the_object;

}

public function has_the_attribute($attribute) {

$object_properties = get_object_vars($this);

return array_key_exists($attribute, $object_properties);
}

最佳答案

你必须使用 PDO::errorInfo():

(...)

public function query($sql, $params = []) {

try {

$query = $this->connection->prepare($sql);
if( !$query )
{
$error = $this->connection->errorInfo();
die( "mysql error: {$error[2]}" );
}

} catch (Exception $e) {
var_dump('mysql error: ' . $e->getMessage());
}

(...)
}

PDO::errorInfo 返回一个数组:

Element 0: SQLSTATE error code (a five characters alphanumeric identifier defined in the ANSI SQL standard);

Element 1: Driver-specific error code;

Element 2: Driver-specific error message.

关于php - 无法在 PHP 中使用 PDO 获取错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34889995/

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