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 :

<?php
$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', '[email protected]','los angeles')";

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

 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 

http://localhost/mysqli/view.php

 


 My SQL Code Segment :

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

//First create database
CREATE DATABASE 'college';
//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

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

<div id="main">
<h1>Insert data into database using mysqli</h1>
<div id="login">
<h2>Student's Form</h2>
<hr/>
<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="[email protected]"/><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 />
</form>
</div>
<!-- Right side div -->
<div id="formget">
<a href=https://www.formget.com/app><img src="formget.jpg" alt="Online Form Builder"/></a>
</div>

</div>
<?php
if(isset($_POST["submit"])){
$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>";
}

$conn->close();
}
?>
</body>
</html>

CSS File : style.css

Styling HTML Elements.


@import url(http://fonts.googleapis.com/css?family=Raleway);

#main{
width:960px;
margin:50px auto;
font-family: 'Raleway', sans-serif;
}

h2{
background-color: #FEFFED;
text-align:center;
border-radius: 10px 10px 0 0;
margin: -10px -40px;
padding: 15px;
}

hr{
border:0;
border-bottom:1px solid #ccc;
margin: 10px -40px;
margin-bottom: 30px;
}

#login{
width:300px;
float: left;
border-radius: 10px;
font-family:raleway;
border: 2px solid #ccc;
padding: 10px 40px 25px;
margin-top: 70px;
}

input[type=text],input[type=email]{
width:99.5%;
padding: 10px;
margin-top: 8px;
border: 1px solid #ccc;
padding-left: 5px;
font-size: 16px;
font-family:raleway;
}

input[type=submit]{
width: 100%;
background-color:#FFBC00;
color: white;
border: 2px solid #FFCB00;
padding: 10px;
font-size:20px;
cursor:pointer;
border-radius: 5px;
margin-bottom: -12px;
}

/*-----------------------------------------------------------------
css settings for right side advertisement
------------------------------------------------------------------*/
#formget{
float:right;
}
h1 {
margin-left: -85px;
}

Conclusion:

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.