Create database using php code?
I try to create Db using sql it's showing "Access Denied user"
Here my code .
$servername = "192.168.0.3";
$username = "root";
$password = "[Moderator Note: Removed]";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
echo "string";
-
I don't understand your script as I don't really know PHP. Did you manually create the user and password (or is that what the script does) 0 -
I'd recommend using the UAPI functions in your code for this: Create the DB user UAPI Functions - Mysql::create_user - Developer Documentation - cPanel Documentation Create the DB UAPI Functions - Mysql::create_database - Developer Documentation - cPanel Documentation Set Privileges for a specific user on a specific database: 0 -
That was the old way with mysqli you have to specify database now. change $conn = new mysqli($servername, $username, $password); TO: $conn = new mysqli($servername, $username, $password, $database); 0 -
The only way I was able to use that script was to change root MySQL password and try it out on the website outside of WHM/cPanel. Remotely root didn't work but I did get it to work successfully. While I was able to create the database WHM/cPanel was not aware the database existed. Not even when the user name was on the prefixed of the database. The final results there is no way outside of WHM/cPanel to create usable databases. If you are creating a custom installer the user will have too create the database within cPanel first and then run the installer with the new user, password, and database information. That is how all the Admins are doing their installs on cPanel. The API'S for WHM will be discountinued in a future release of WHM/cPanel. I Just don't know the date. 0
Please sign in to leave a comment.
Comments
4 comments