こんにちは、データベース ユーザーを作成し、そのユーザーのデータベースを作成し、そのデータベースのテーブルを適切な順序で作成する 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);
}
}