gpt4 book ai didi

php - 查询字符串中未捕获异常

转载 作者:行者123 更新时间:2023-11-30 01:11:30 25 4
gpt4 key购买 nike

您好,我有一个 php 注册脚本,它创建一个数据库用户,然后为该用户创建一个数据库,然后按适当的顺序为该数据库创建表。

1 个字符串中包含一系列查询。以下是执行的查询列表:http://pastebin.com/REBMFyyT正如您所看到的,我故意漏输入了“创建表客户端”查询来检查我的自定义错误日志记录。问题是异常没有被捕获。这是负责的函数:

public function register() {

global $session;
global $cookie;

$credentials = array('username' => $this->getPostUsername(), 'password' => $this->getPostPassword());

if (!$this->areValid($credentials)) {
echo 'Invalid credentials!';
return false;
}

if ($this->userExists($credentials['username'])) {
echo 'User already exists!';
return false;
}

try{

$this->rootConnection->beginTransaction();

// create user record
$statement1 = $this->rootConnection->prepare($this->createUserRecord());
$username = $credentials['username'];
$password = MD5($credentials['password'] . $this->salt);
$statement1->bindParam(':username', $username);
$statement1->bindParam(':password', $password);
$statement1->execute();

$user_id = $this->rootConnection->lastInsertId();
$user_database = "user_$user_id";
$db_username = "user_$user_id";
$db_password = MD5($db_username . $this->salt);

// update user database info
$statement2 = $this->rootConnection->prepare($this->updateUserDbInfo());
$statement2->bindParam(':database', $user_database);
$statement2->bindParam(':db_username', $db_username);
$statement2->bindParam(':db_password', $db_password);
$statement2->bindParam(':id', $user_id);
$statement2->execute();

$this->rootConnection->commit();

} catch (Exception $e) {
$this->log($e->getMessage());
return false;
}


$userData = $this->getUserById($user_id);

$session->set($userData);
$cookie->set('username', $username);
$cookie->set('password', $password);

$megaQuery = $this->createDatabaseUser() . ";\n" .
$this->grantUserUsage() . ";\n" .
$this->createUserDatabase($user_database) . ";\n" .
$this->grantUserDatabasePrivileges() . ";\n" .
$this->createDriversTable($user_database) . ";\n" .
$this->createClientsTable($user_database) . ";\n" .
$this->createCarsTable($user_database) . ";\n" .
$this->createRecordsTable($user_database);

try{

$statement3 = $this->rootConnection->prepare($megaQuery);
$statement3->bindParam(':db_username', $db_username);
$statement3->bindParam(':db_password', $db_password);
$statement3->execute();

return true;

} catch(Exception $e) {
$this->log($e->getMessage());
return false;
}

return false;

}

此外,这就是我初始化数据库连接的方式:

private function connect(){
$this->rootConnection = new PDO("mysql:host=$this->rootHost;dbname=$this->rootDatabase", $this->rootUsername, $this->rootPassword, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")) or die("Manager error connecting to database!");
$this->rootConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->rootConnection->setAttribute(PDO::ATTR_PERSISTENT, true);
}

我的问题很明显,为什么异常没有被捕获并进一步记录?PS:变量 megaQuery 包含并合并 MySql 上的权限、数据库和用户创建的查询,这些查询不是“可事务”查询。在这种情况下,文件系统权限也不是问题。最后是日志功能:

public function log($data, $file = 'log/errors.log') {

$now = $today = date("Y-m-d H:i:s");
$data = "[$now]\t$data\n";

if (file_exists($file)) {
echo 'file exists<br>';
file_put_contents($file, $data, FILE_APPEND);
} else {
echo 'file will be created<br>';
file_put_contents($file, $data);
}

}

最佳答案

不阅读问题正文(无论如何,这听起来太本地化了),只是一个 list

捕获异常:

  1. 必须抛出异常。如果是 PDO,则必须是 configured to do so.
    • 当然,异常应该有一个原因
  2. 如果使用命名空间,则必须使用根路径,例如\PDOException
  3. 代码中没有拼写错误。

关于php - 查询字符串中未捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19444599/

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