1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <?php define('DB_HOST', '127.0.0.1');
define('DB_ROOT_USER', 'root'); define('DB_ROOT_PASS', 'rootpass');
$dbname = 'my_new_db';
$dbuser = 'my_new_db_user'; $dbpass = 'new_dbpassword';
try { $dbh = new PDO('mysql:host='.DB_HOST, DB_ROOT_USER, DB_ROOT_PASS);
$dbh->exec( "CREATE DATABASE `$dbname`; CREATE USER '$dbuser'@'localhost' IDENTIFIED BY '$dbpass'; GRANT ALL ON `$dbname`.* TO '$dbuser'@'localhost'; FLUSH PRIVILEGES;" ) or die(print_r($dbh->errorInfo(), true));
$dbh = new PDO('mysql:host='.DB_HOST.';dbname='.$dbname, DB_ROOT_USER, DB_ROOT_PASS);
$imported = $dbh->exec(file_get_contents('existingdata.sql')); if ($imported === false) { die(print_r($dbh->errorInfo(), true)); }
} catch (PDOException $e) { die("DB ERROR: ". $e->getMessage()); }
|