PHP and MySQLi Create Database

« Previous Tutorial Next Tutorial »

To create MySQL database using PHP with MySQLi, following are the two ways:

Let's learn about the above two ways to create MySQL database, that is, by using PHP with MySQLi or by manual operation.

Create MySQL Database Manually

To create MySQL database manually, follow the following steps:

  1. Open XAMPP
  2. Start Apache Module
  3. Start MySQL Module
  4. Click on Admin of MySQL Module
  5. Click on Database link
  6. Enter Database name say codescracker in the Create database input box
  7. Click on Create button

Here is the screenshot of the window that will appear after performing the 4th option.

php mysqli create database

And below is the screenshot of the window that will appear after performing the 7th (last) option

php mysqli create database example

Now you have created a database named codescracker recently. Now let's learn how to create MySQL database using PHP with Mysqli.

Create MySQL Database using PHP with MySQLi

Here is an example shows how to create MySQL database using PHP with MySQLi.

	// below code used to connect to the server 
	// here localhost is the local server name
	// root is the local username of the XAMPP
	// and password is blank
	$conn = mysqli_connect('localhost', 'root', '');
	// below code will check whether the server connection 
	// is successfully done or not
		echo "<p>Error occurred while connecting to the server.</p>";
		echo "<p>Please visit us later.</p>";
		echo "<p>Exiting....</p>";
	<title>PHP and MySQLi Create Database Example</title>
	// place the database name in some variable 
	// say $databaseName here
	$databaseName = "codescrackerTwo";
	// place the query in another variable
	// say $querySQL here
	$querySQL = "create database $databaseName";
	// now query the above SQL statement and 
	// store the returned result into another variable 
	// say $returnRes here to check whether the 
	// query has done successfully or not
	$returnRes = $conn->query($querySQL);
		// query has done successfully
		echo "<p>Database <b>codescrackerTwo</b> is created successfully.</p>";
		// error occurred in querying the above given sql statement
		echo "<p>Error occurred while creating the database.</p>";
		echo "<p>Exiting....</p>";

Now type the above code or just do copy and past in your text editor, and save the above file inside the directory C:\xampp\htdocs\ with name codescracker.php and open your browser then type localhost/codescracker.php

After performing the above action, you have successfully run the above given example code of creating MySQL database using PHP with MySQLi and created a MySQL database named codescrackerTwo.

Here is the sample output produced by the above PHP and MySQLi create database example code:

create database php mysqli

To see the created database, just click on the Database link, here is the screenshot after clicking on the database link.

php mysqli create database code

And to see the database link at the left pannel, then close the browser and again click on the Admin button of the MySQL module in XAMPP. Here is the screenshot of the browser window, that is, phpMyAdmin window after clicking on the Admin button:

create mysql database using php

As you can see your database name, that is, codescrackerTwo is present in the left pannel of the phpMyAdmin window.

« Previous Tutorial Next Tutorial »


Quick Links
Signup - Login - Give Online Test