php script automatically creating mysql database?
does anyone know if there's a script out there that will create a mysql database automatically instead of having to go into cpanel and create a 开发者_如何学JAVAdatabase, username and password manually?
thanks
You cannot create a username and password unless you log in yourself with a user with a higher level. That's usually the root. Creating database is a breeze after using the username and password with sufficient privileges.
<?php
$dsn = $dsn = "mysql:host=localhost";
$pdo = new PDO($dsn,"root","");
//Creation of user "user_name"
$pdo->query("CREATE USER 'user_name'@'%' IDENTIFIED BY 'pass_word';");
//Creation of database "new_db"
$pdo->query("CREATE DATABASE `new_db`;");
//Adding all privileges on our newly created database
$pdo->query("GRANT ALL PRIVILEGES on `new_db`.* TO 'user_name'@'%';");
?>
In this script, I assumed your root user is called "root" and its password is empty if that's not the case, change line 4 to match.
you cannot create new user without logging in.
You can create database with root account or user with privileges
<?php
mysql_connect("localhost", "root", "gm123") or die(mysql_error());
mysql_query("CREATE DATABASE cat") or die(mysql_error());
mysql_select_db("cat") or die(mysql_error());
mysql_query("CREATE TABLE user(username varchar(20), password varchar(20), permission varchar(20))") or die(mysql_error());
mysql_query("INSERT INTO user(username,password,permission) VALUES('gm','311807','admin')") or die(mysql_error());
mysql_query("CREATE TABLE cattbl(id INT NOT NULL PRIMARY KEY ,name VARCHAR(30),roll INT NOT NULL,technology VARCHAR(30),semister VARCHAR(30),shift VARCHAR(30),bdate varchar(30),cell VARCHAR(30),address VARCHAR(30),picture LONGBLOB)") or die(mysql_error());
header("Location: index.php");
?>
you may try this for your problem
i have just been doing this on my project
the actual answer is you can but it's a little complicated.
Cpanel doesnt allow you to use the normal "CREATE DATABASE 'mydbname';" in a php script to create a new database.
the only way you can do it is via logging on to your cpanel, going through the database wizard and creating a new one. Up until now you have probably been doing this manually each time.
this can been done with a php script but you have to use an api - which kind of goes through those same actions but automatically. You have to give the api your cpanel username and cpanel password.
just follow the answer to this question Create cpanel database through php script
hope that helps
require_once 'database.php' add this line of code your index.php
$servername = 'localhost';
$username= 'root';
$password ='';
$conn = new mysqli($servername,$username,$password);
if($conn->connect_error)
{
die("Connection Failed !" . $conn->connect_error());
}
else
{
$sql = "CREATE DATABASE test_employee";
if($conn->query($sql)== TRUE )
{
echo "Database Created Successfully";
}
}
You definitely can! You can use PHP.
First you need to connect to the database. Then you need to run a SQL query on that database that creates the database. (which is essentially what cpanel does)
EDIT: Updated since my_sql is deprecated
Heres a stackoverflow post on how to do it...
Can I create a database using PDO in PHP
精彩评论