- PHP Basics
- PHP Home
- PHP Environment Setup
- PHP Getting Started
- PHP Basic Syntax
- PHP echo
- PHP print
- PHP echo Vs print
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Variable Scope
- PHP gettype()
- PHP Constants
- PHP Operators
- PHP Program Control
- PHP Decision Making
- PHP if-elseif-else
- PHP switch
- PHP Loops
- PHP for Loop
- PHP while Loop
- PHP do-while Loop
- PHP foreach Loop
- PHP break & continue
- PHP Popular Topics
- PHP Arrays
- PHP print_r()
- PHP Strings
- PHP Functions
- PHP References
- PHP Object Oriented
- PHP Object Oriented
- PHP Classes & Objects
- PHP Member Variable
- PHP Member Function
- PHP Encapsulation
- PHP Data Abstraction
- PHP Inheritance
- PHP Constructor Destructor
- PHP Polymorphism
- PHP Web Developments
- PHP Web Developments
- PHP GET & POST
- PHP Read Requested Data
- PHP File Handling (I/O)
- PHP File Handling (I/O)
- PHP fopen() | Open File
- PHP Create a File
- PHP fwrite() | Write to File
- PHP fread() | Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP fclose() | Close File
- PHP unlink() | Delete File
- PHP Append to File
- PHP copy() | Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP rename() | Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP mkdir() | Create Directory
- PHP rmdir() | Remove Directory
- PHP glob() | Get Files/Directories
- PHP basename() | Get filename
- PHP dirname() | Get Path
- PHP filemtime()
- PHP file()
- PHP Advanced
- PHP Cookies
- PHP Sessions
- PHP Send Emails
- PHP Serialization
- PHP Namespaces
- PHP File Upload
- PHP Date and Time
- PHP Image Processing
- PHP Regular Expression
- PHP Predefined Variables
- PHP Error Handling
- PHP Debugging
- PHP and MySQLi Tutorial
- PHP and MySQLi Home
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Fetch Record
- PHP MySQLi Update Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP connect_errno
- PHP connect_error
- PHP query()
- PHP fetch_row()
- PHP fetch_assoc()
- PHP fetch_array()
- PHP free_result()
- PHP error
- PHP prepare()
- PHP bind_param()
- PHP execute()
- PHP fetch()
- PHP store_result()
- PHP num_rows
- PHP bind_result()
- PHP get_result()
- PHP mysqli_result Class
- PHP Error Constants
- PHP mysqli_driver()
- PHP Misc
- PHP error_reporting()
- PHP Escape Special Characters
- PHP htmlspecialchars()
- PHP new
- PHP header()
- PHP getallheaders()
- PHP empty()
- PHP isset()
- PHP unset()
- PHP exit()
- PHP exit Vs break
- PHP include()
- PHP require()
- PHP include() Vs require()
- PHP AJAX & XML
- PHP AJAX
- PHP XML
- PHP File Handling Functions
- PHP abs()
- PHP Test
- PHP Online Test
- Give Online Test
- All Test List
PHP MySQLi Create Database
A database consists of tables. A table stores information in the form of rows and columns. Therefore, to store information in your database, you need to create a database first.
We can follow any of the following approaches to create a database in our MySQL database server:
- Using Manually
- Using PHP MySQLi Script
Note - A database consists of one or more tables. A table consists of one or more rows (records), where each row consists of one or more columns (fields).
PHP MySQL Create Database - Manually
Since in previous tutorial, I've already mentioned that, I'm going to use XAMPP, that provides MySQL database to work with. Therefore to create MySQL database, manually, follow these steps:
Step No.1: Open XAMPP Control Panel.
Step No.2: Click on Start, next to Apache.
Step No.3: Click on Start, next to MySQL.
Step No.4: Now click on Admin, next to MySQL, as shown in the snapshot of the XAMPP Control Panel Window given below:
Step No.5: The window opened after clicking on the Admin, as directed above, looks like:
Step No.6: Click on the Databases available at top left on the top menus, like shown in the snapshot given below:
Step No.7: Enter the name of the database say codescracker and click on the Create button, like shown in the snapshot given below:
The database named codescracker is created successfully. If you see on the left menu, there codescracker can be seen every time, when you open the MySQL Databases.
Create MySQL Database using PHP MySQLi Object-Oriented Script
To create a MySQL database using PHP MySQLi object-oriented script, follow the example given below:
<?php $servername = "localhost"; $username = "root"; $password = ""; // Opens a connection to the Database $conn = new mysqli($servername, $username, $password); // Checks the connection if($conn->connect_errno) { echo "Database connection failed!<BR>"; echo "Reason: ", $conn->connect_error; exit(); } // Creates a database named student $sql = "CREATE DATABASE student"; $qry = $conn->query($sql); if($qry) { echo "Database created successfully."; // block of code, to process further... } else { echo "Database has not been created!!<BR>"; echo "Reason: ", $conn->error; } $conn->close(); ?>
The output produced by above PHP example on creating a database using PHP MySQLi script, is shown in the snapshot given below:
Now if you open the database server, then a new database named student, will be available there.
Now let me re-execute the above PHP script again. Okay, here is the output I have got:
Since the database named student was already created, therefore we are seeing this output. But the problem is, the error message displayed, is not as I have written in script. That is because of default error reporting mode.
To get the manual written error message to be displayed, instead of the default one, we need to change the error reporting mode, using either mysqli_driver() (for object-oriented) or mysqli_report() (for procedural). For example:
<?php $driver = new mysqli_driver(); $driver -> report_mode = MYSQLI_REPORT_OFF; $conn = new mysqli("localhost", "root", ""); if(!$conn->connect_errno) { if($conn->query("CREATE DATABASE student")) echo "Database created successfully."; else echo "Error Occurred<BR>Reason: ", $conn->error; } $conn->close(); ?>
Now the output should be:
Error Occurred Reason: Can't create database 'student'; database exists
Important - Since, we only need to provide the first three arguments to the mysqli() function, to connect to the database server. Therefore, if you want to use a specific port, to connect through. Then let me tell you, you need to add/specify that port number always on the fifth parameter. Therefore just give empty string, that is "" as fourth parameter (used for database name), then use fifth parameter to specify the port number. For example:
$conn = new mysqli("localhost", "root", "", "", 67);
to connect to the database server at port number 67.
Note - The mysqli() is used to open a connection to the MySQL database server, in object-oriented style.
Note - The new keyword is used to create a new object.
Note - The connect_errno is used to get/return the error code (if any) from last connect call, in object-oriented style.
Note - The connect_error is used to get the error description (if any) from last connection, in object-oriented style.
Note - The query() is used to perform query on the MySQL database, in object-oriented style.
Note - The error is used to return the description of error (if any), by the most recent function call, in object-oriented style.
Note - The close() is used to close an opened connection, in object-oriented style.
Note - The mysqli_driver() is used to modify the error reporting mode, in object-oriented style.
Create MySQL Database using PHP MySQLi Procedural Script
To create a MySQL database using PHP MySQLi procedural script, here is an example, you need to follow:
<?php mysqli_report(MYSQLI_REPORT_OFF); $conn = mysqli_connect("localhost", "root", ""); if($conn) { if(mysqli_query($conn, "CREATE DATABASE student")) echo "Database created successfully."; else echo "Error Occurred<BR>Reason: ", mysqli_error($conn); } mysqli_close($conn); ?>
This script does the same job as of previous one.
Note - The mysqli_report() is used to modify the error reporting mode, in procedural style.
Note - The mysqli_connect() is used to open a connection to the MySQL database server, in procedural style.
Note - The mysqli_connect_errno() is used to get/return the error code (if any) from last connect call, in procedural style.
Note - The mysqli_error() is used to return the description of error (if any), by the most recent function call, in object-oriented style.
Note - The mysqli_close() is used to close an opened connection to the MySQL database, in procedural style.
« Previous Tutorial Next Tutorial »