• Home
  • Uncategorized
  • Create a new Mysql database, create a new user and grant privileges on php

Create a new Mysql database, create a new user and grant privileges on php

<?php
print "<html><body>";
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'nicole1983';

if(isset($_POST['database_name']) && ($database_name = $_POST['database_name'] <> "" )) 
{
	print "About to create database..";
	$database_name = $_POST['database_name'];

	$conn = mysql_connect($dbhost, $dbuser, $dbpass);
	if(! $conn )
	{
	  die('Could not connect: ' . mysql_error());
	}
	#echo 'Connected successfully';
	$sql = 'CREATE Database '.$database_name;
	$retval = mysql_query( $sql, $conn );
	if(! $retval )
	{
	  die('Could not create database: ' . mysql_error());
	}
	print "Database ".$database_name." created successfully\n";

	if(isset($_POST['database_user']) && ($database_user = $_POST['database_user'] <> "" )) 
	{
		$database_user = $_POST['database_user'];
		$sql = "CREATE USER '".$database_user."'@'localhost' IDENTIFIED BY 'secret';";
		$retval = mysql_query( $sql, $conn );
		if(! $retval )
		{
		  die('Could not create database: ' . mysql_error());
		}
		print "User ".$database_user." created successfully\n";
		$sql = "GRANT ALL ON ".$database_name.".* TO '".$database_user."'@'localhost';";
		$retval = mysql_query( $sql, $conn );
		if(! $retval )
		{
		  die('Could not grant privileges to user: ' . mysql_error());
		}
		print "Granted privieleges successfully...\n";
	}
	mysql_close($conn);
}
else
{
	print "<p>You didnt specify a database to create. Nothing will be done</p>";
	print '<p><a href="./createtable.html">Try again</a></p>';
}
print "</body></html>";
?>