• support@answerspoint.com

How to create a MySQL database using PHP?

2207

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

1Answer


0

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 );

<html>
<head>
<title>Database Creation</title>
</head>
    <body>
          <?php
             $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";
             mysql_close($conn);
          ?>
    </body>
</html>

 

  • answered 7 years ago
  • Community  wiki

Your Answer

    Facebook Share        
       
  • asked 7 years ago
  • viewed 2207 times
  • active 7 years ago

Best Rated Questions