&n"/> CreatingMySQLDatabase &n">

Home >Backend Development >PHP Tutorial >How can we create a new database using PHP script?

How can we create a new database using PHP script?

WBOY
WBOYforward
2023-08-26 17:33:061057browse

How can we create a new database using PHP script?

As we all know, PHP provides us with a function called mysql_query to create a new database.

Example

To illustrate this, in the following example, we will create a database named "Tutorials" with the help of a PHP script -

<html>
   <head>
      <title>Creating MySQL Database</title>
   </head>
   <body>
      <?php
         $dbhost = &#39;localhost:3036&#39;;
         $dbuser = &#39;root&#39;;
         $dbpass = &#39;rootpassword&#39;;
         $conn = mysql_connect($dbhost, $dbuser, $dbpass);
         
         if(! $conn ) {
            die(&#39;Could not connect: &#39; . mysql_error());
         }
         echo &#39;Connected successfully<br />&#39;;
         $sql = &#39;CREATE DATABASE TUTORIALS&#39;;
         $retval = mysql_query( $sql, $conn );
         
         if(! $retval ) {
            die(&#39;Could not create database: &#39; . mysql_error());
         }
         echo "Database TUTORIALS created successfully</p><p>";
         mysql_close($conn);
      ?>
   </body>
</html>

The above is the detailed content of How can we create a new database using PHP script?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete