PHP MySQLi, where i denotes to “Improved”. Using MySQLi function you can access the database server. Mysqli is the enhanced version of Mysql. 

In this tutorial we are demonstrate how we can use MySQLi to access mysql database server.

Before accessing the MySQL database.First we need to connect them using MYSQLi .

Connection to MySQL Using MySQLi

Here we are using MySQLi Object-Oriented method to Open a Connection to MySQL.

Syntax :

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

// Create connection $conn = new mysqli($servername, $username, $password,$dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; ?>

Insert Data Using MySQLi

preparing a insert query and run query by connection object.

Syntax :

$sql = "INSERT INTO students (student_name, student_email, student_city)
VALUES ('John', '','los angeles')";

if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;

 You can also refer our live demo or download the Script file. Extract the downloaded files, save it in your local server and run it using given path 


 insert data on database using mysqli

 My SQL Code Segment :

To create database and table, execute following codes in your My SQL.

//First create database
//Then create table
CREATE TABLE 'students' (
'student_id' int(11) NOT NULL AUTO_INCREMENT,
'student_name' varchar(255) NOT NULL,
'student_email' varchar(255) NOT NULL,
'student_city' varchar(255) NOT NULL,
PRIMARY KEY ('student_id')

Php File : view.php

In view.php file ,we are using student information form there are only three filed student name, email and address.Using mysqli functions we are inserting data on student table.

Copy the below code in your file and save as view.php

<title>insert data in database using mysqli</title>
<link rel="stylesheet" type="text/css" href="style.css">

<div id="main">
<h1>Insert data into database using mysqli</h1>
<div id="login">
<h2>Student's Form</h2>
<form action="" method="post">
<label>Student Name :</label>
<input type="text" name="stu_name" id="name" required="required" placeholder="Please Enter Name"/><br /><br />
<label>Student Email :</label>
<input type="email" name="stu_email" id="email" required="required" placeholder=""/><br/><br />
<label>Student City :</label>
<input type="text" name="stu_city" id="city" required="required" placeholder="Please Enter Your City"/><br/><br />
<input type="submit" value=" Submit " name="submit"/><br />
<!-- Right side div -->
<div id="formget">
<a href=><img src="formget.jpg" alt="Online Form Builder"/></a>

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "college";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);

$sql = "INSERT INTO students (student_name, student_email, student_city)
VALUES ('".$_POST["stu_name"]."','".$_POST["stu_email"]."','".$_POST["stu_city"]."')";

if ($conn->query($sql) === TRUE) {
echo "<script type= 'text/javascript'>alert('New record created successfully');</script>";
} else {
echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";


CSS File : style.css

Styling HTML Elements.

@import url(;

margin:50px auto;
font-family: 'Raleway', sans-serif;

background-color: #FEFFED;
border-radius: 10px 10px 0 0;
margin: -10px -40px;
padding: 15px;

border-bottom:1px solid #ccc;
margin: 10px -40px;
margin-bottom: 30px;

float: left;
border-radius: 10px;
border: 2px solid #ccc;
padding: 10px 40px 25px;
margin-top: 70px;

padding: 10px;
margin-top: 8px;
border: 1px solid #ccc;
padding-left: 5px;
font-size: 16px;

width: 100%;
color: white;
border: 2px solid #FFCB00;
padding: 10px;
border-radius: 5px;
margin-bottom: -12px;

css settings for right side advertisement
h1 {
margin-left: -85px;


So, this was all about accessing database server using php mysqli.Hope you might have understood it properly, keep reading our other blogs posts for more coding tricks.