您好,我有一个 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);
}
}