你好,我有一個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);
}
}
是否提供有效的SQL時,查詢成功運行? – Ryan
是的,他們工作得很好。我的問題是,如果查詢無效,則會發生異常 - 格式錯誤,mySQL返回並顯示錯誤。在這種情況下,我錯誤地將'創建表'創建爲'創建tabdles',但沒有拋出異常。 – Syd