How to create a MySQL database using PHP?


I am trying to create a database using PHP and MySQL. But it fails to create. Can anyone help me?



The CREATE DATABASE statement is used to create a database in MySQL.

and PHP uses mysql_query function to create or delete a MySQL database. This function takes two parameters.

mysql_query( sql, connection );

<title>Database Creation</title>
             $dbhost = 'localhost';
             $dbuser = 'root';
             $dbpass = '';
             $conn = mysql_connect($dbhost, $dbuser, $dbpass);
             if(! $conn ) {
                die('Could not connect: ' . mysql_error());
             echo 'Connected successfully<br />';
             $sql = 'CREATE DATABASE answerspoint';
             $retval = mysql_query( $sql, $conn );
             if(! $retval ) {
                die('Could not create database: ' . mysql_error());
             echo "Database answerspoint created successfully";


  • answered 2 years ago
  • Community  wiki

Your Answer

    Facebook Share        
  • asked 2 years ago
  • viewed 994 times
  • active 2 years ago

Hot Questions