Hello Friends,
There is a new problem with me please help me guys.
The Problem is that How to create database and database user in mysql using php?
Hello Friends,
There is a new problem with me please help me guys.
The Problem is that How to create database and database user in mysql using php?
To create a database you’d need to run an SQL query like:
CREATE DATABASE my_db
As for a user I think that will be pretty difficult; you might find it easier to use a DBMS like PhpMyAdmin to create the user.
Hello Friend,
I was already do this but in my local wamp server this code is working correctly but on my website server it’s not working.
There is the code which i m using for creating database and user.
If I my code is wrong please correct it.
<?
if(isset($_POST['create']))
{
extract($_POST);
$content .= "
<?
session_start();
define('DB_SERVER', '".$dbhost."');
define('DB_USERNAME', '".$uname."');
define('DB_PASSWORD', '".$pwd."');
define('DB_DATABASE', '".$dbname."');
class DB_Class
{
function __construct()
{
mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or
die('Oops connection error -> ' . mysql_error());
mysql_select_db(DB_DATABASE)
or die('Database error -> ' . mysql_error());
}
} ?>";
$fp = fopen("../admin/include/config.php","wb");
fwrite($fp,$content);
fclose($fp);
$conn = mysql_connect($dbhost, $uname, $pwd);
/*if(! $conn )
{
$sql = mysql_query( 'CREATE DATABASE '.$dbname.'');
//$createQ = "CREATE USER '{$uname}'@'{$dbhost}' IDENTIFIED BY '{$pwd}'";
}*/
$retval = mysql_query($conn );
if(! $retval )
{
$sql = mysql_query( 'CREATE DATABASE '.$dbname.'');
$createQ = mysql_query("CREATE USER '{$uname}'@'{$dbhost}' IDENTIFIED BY '{$pwd}'");
mysql_query("GRANT ALL ON '".$dbname."'.* TO '$uname'@'localhost'");
}
header('location: step-2.php');
}
?>
Thank you helping me.
Does your online web server support the phpmysql extension?
Look at using PDO (or even mysqli) instead of mysql functions.
What is the error returned?
What do you mean by “my website server”? Is it a server that you own or something that you rent with, for example, cpanel? The security on those shared server are normally really tight and you probably have a limit of number of databases that you can create.
Just by curiosity, why do you want to create multiple databases like this?
… and many don’t allow creating a database let alone a DB user with a script. You have to use cpanel or whatever program they provide. Been down that road before.