Skip to main content
PHP–MySQL Connection
using MySQLi
Prepared By:-
Kale J. N.
Assistant Professor,
Computer Engineering,
Sanjivani College of Engineering, Kopargaon
Prepared By: Kale J. N.
1. Introduction
• Most websites need to store and retrieve data such as:
• Student records
• User login information
• Registration details
• Product information
• This data is stored in a database. PHP can interact with a database
using MySQLi.
• Therefore, PHP–MySQL connection is required so that PHP programs
can communicate with the MySQL database.
Prepared By: Kale J. N.
2. What is MySQLi?
• MySQLi stands for:
• MySQL Improved
• It is a PHP extension used to connect PHP with MySQL database.
• Features of MySQLi:
• Faster than old MySQL extension
• Supports prepared statements
• Supports transactions
• Supports both procedural and object-oriented programming
Prepared By: Kale J. N.
3. Requirements for PHP–MySQL Connection
• Before connecting PHP with
MySQL, the following are
required:
• Web Server (Apache)
• PHP installed
• MySQL Database
• Common development
environments include:
• XAMPP
• WampServer
• These packages contain Apache
+ PHP + MySQL.
Prepared By: Kale J. N.
4. Steps to Connect PHP with MySQL
Prepared By: Kale J. N.
5. Syntax of MySQLi Connection
• mysqli_connect(servername,
username, password, database)
Parameter Description
servername Location of database server
username MySQL username
password MySQL password
database Name of database
Prepared By: Kale J. N.
6. PHP–MySQL Connection Program
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "college";
$conn = mysqli_connect($servername,
$username, $password, $database);
Prepared By: Kale J. N.
if(!$conn)
{
die("Connection Failed");
}
else
{
echo "Database Connected
Successfully";
}
?>
• Step 1: Server Name
• Step 2: Username
• Step 3: Password
• Step 4: Database Name
• Step 5: Creating Connection
• Step 6: Checking Connection
• die() stops the program and prints
the message.
• Step 7: Successful Connection
Prepared By: Kale J. N.
7. Closing Database Connection
• After finishing database
operations, the connection
should be closed.
• mysqli_close($conn);
• Example:
• mysqli_close($conn);
Prepared By: Kale J. N.