简体   繁体   中英

PHP - Creating MySQL database and user - code not working

I am developing an application where multi-tenancy is not suitable. So my plan is to create different MySQL databases and users for each client account. I am having problems, if you can see what's wrong with my code that would be great - I am not getting any errors, but the database/user is not being added .

$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));

I noticed you said you were using cPanel. Check phpMyAdmin to see if you can create users. If the tab is available then your code should work as expected. If you don't see if then you can check your configuration options in the phpMyAdmin config.inc.php file. I'm an uncertain if the issue resides with cPanel or phpMyAdmin. As for your code, it works 100% on XAMPP running on Windows so the problem lies elsewhere. Sorry I couldn't give a definitive answer!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM