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.

//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

This new mysqli() function is establish 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 structure
database table structure

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