Thursday, October 12, 2017

anil

How To Insert Records Using Stored Procedure in MySql with PHP ?

If you want to insert large records in a table so it is great to do that by using of Stored Procedure. There are number of benefits to insert record with the use of Stored Procedure

  • Stored procedure cached on server so the speed is more fast.
  •  A Stored Procedures will be in one place so that there’s no confusion of having business rules spread over potentially disparate code files in the project.
  • More secure because there is no direct access with tables in database.
I am describing the process of inserting records in database by using Stored Procedure as below.

Step #1 : Create a Stored Procedure in your database.
                
you can create a Stored Procedure by two way.

1. Create a stored procedure by using of phpMyAdmin.

2. Create a Stored Procedure by using MySql query.

you can use below query to create a stored procedure to insert data.

DELIMITER $$
CREATE  PROCEDURE `insert_records`(IN `name` VARCHAR(50))
BEGIN
insert into r (name) values (name);
END$$
DELIMITER ;

Step #2 : Now Create table in which we want to insert data.

create table by using below query -

CREATE TABLE `insert_records`(`name` VARCHAR(100))


Step #3 : Call Stored Procedure by PHP script to insert names.

you can call a stored procedure by using below query to insert data -

<?php 
     $name = array('anil','raj','sonia','monica','sivani');
    foreach($name  as $key=>$val){ 
$data = implode("','",$val);
echo '===== Standard data insertion start ===== <br/>'.$data.'<br/>';
$query = "CALL insert_records('".$data."')";
try{
            $success = $conn->query($query);
            if($success){
                echo "inserted succesfully";             
            }
            else{
                    echo "Data not successfully Inserted.";
            }
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>

When you run the script it will insert the all data in the table and if there is any error it will show exception.

you can easily insert data into database by using this process.


About Author -

Hi, I am Anil.

Welcome to my eponymous blog! I am passionate about web programming. Here you will find a huge information on web development, web design, PHP, Python, Digital Marketing and Latest technology.

Subscribe to this Blog via Email :

Note: Only a member of this blog may post a comment.