Insert Data MySQL Database Using Object Oriented Method

In this tutorial how to insert data in a MySQL table using Object Oriented Method in PHP.

MySQL INSERT Query using Object Oriented Method

We will discuss PHP Object Oriented Method. Example is given below.

<?php
//Database connection
$con = new mysqli("localhost", "database user", "database Password", "database name");
 
// Check connection
if($con === false){
    die("ERROR: Could not connect. " . $con->connect_error);
}
 
// insert query execution
$sql = "INSERT INTO member(name,addr,email) VALUES ('saurav', 'kolkata', '[email protected]')";
if($con->query($sql) === true){
    echo "Records inserted successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . $con->error;
}
 
// Close connection
$con->close();
?>

This new mysqli() function is to establish a database connection. After if condition check database connection. INSERT INTO sql query insert data Mysql database table. Then if condition execution insert query and connection close.

Database Structure example is given below

Database Table
Database Table

Three methods are writing in INSERT INTO statement. Procedural Method as we discussed earlier. Next tutorial PDO Method.

Saurav Sen

Saurav Sen

Hi, I am a professional blogger and web developer. So I also share experiences with web development tutorials and blogging tips and including writing about HTML, CSS, JavaScript, Jquery, Ajax, PHP, and MySQL.