我正在開發一個多租戶不適用的應用程序。所以我的計劃是爲每個客戶帳戶創建不同的MySQL數據庫和用戶。我有問題,如果你能看到我的代碼有什麼問題,那將會很棒 - 我沒有收到任何錯誤,但是數據庫/用戶沒有被添加。PHP - 創建MySQL數據庫和用戶代碼不起作用
$dsn = "mysql:host=localhost";
$pdo = new PDO($dsn,"REMOVED","REMOVED"); // I removed the login.
$username = 'testing';
$password = 'password';
$dbname = 'test_table';
//Creation of user
$STH = $pdo->prepare("CREATE USER `:username` @'%' IDENTIFIED BY `:password`;");
$STH->execute(array(':username' => $username, ':password' => $password));
//Creation of database "new_db"
$STH = $pdo->prepare("CREATE DATABASE `:dbname`;");
$STH->execute(array(':dbname' => $dbname));
//Adding all privileges on our newly created database
$STH = $pdo->prepare("GRANT ALL PRIVILEGES on `:dbname`.* TO `:username`@'%';");
$STH->execute(array(':dbname' => $dbname,':username' => $username));
您的「已刪除」用戶是否擁有CREATE USER,CREATE DATABASE,GRANT操作的權限? – kirugan
我使用的是cPanel創建的用戶,我確信這個用戶有權限。 – cantaffordretail
我在代碼片段中看不到錯誤測試/處理代碼,您確定它正確執行其職責嗎? – fvu