Inserting multiple records in to table with php

As we inserted , we can insert multiple record into the table. The string concatenation operator(.=) allows to execute multiple sql commands in a single php code.

<?php
$servername="localhost";
$username="root";
$password="";
$dbname="mydb";
//connecting...
$conn=new mysqli($servername,$username,$password,$dbname);
//checking...
if($conn->connect_error)
{
    die("connection failed:".$conn->connect_error);
}
//inserting into the table
$sql="INSERT INTO firsttable VALUES(4,'febin');";
$sql .="INSERT INTO firsttable VALUES(5,'ashly');";
$sql .="INSERT INTO firsttable VALUES(6,'hyfa');";
$sql .="INSERT INTO firsttable VALUES(7,'tisha');";

if($conn->multi_query($sql)===TRUE)
{
    echo "new records inserted sucessfully";

}
else
{
    echo "error :".$conn->error;
}

$conn->close();
?>


We can insert any number of queries with this Concatenation operator. It concatinates all the sql queries and starts to execute the sql commands together.

Output:
new rows inserted sucessfully


Prev
Next

Comments

Popular posts from this blog

Node.js Cheat Sheet

Codeigniter ! Simple But Powerful

Bootstrap ? What is it ?