Stored Procedure In PHP

A stored procedure is a set of SQL commands that have been compiled and stored on the database server.
Once the stored procedure has been “stored”, client applications can execute the stored procedure over and over again without sending it to the database server again and without compiling it again.
Stored procedures improve performance by reducing network traffic and CPU  load.
Comparison with dynamic SQL

  • Remove overhead
  • Avoidance of network  traffic
  • Encapsulation of business  logic
  • Delegation of access-rights
  • Some protection from SQL injection attacks

How To Create And Execute SP
There are various options that can be used to create stored procedures.  In these next few topics we will discuss creating a   stored procedure  and How to execute.
Syntax –

Create   Proc   SP_Name
	Param1 	DataType,
	param2	DataType,
	Param3 	DataType,
	Paramn DataType
		Body Of Store Procedure

How to insert Data into Database Using Stored Procedure
Create a Sql Table with name user. Structure of the user table given below….

  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `contactno` bigint(11) NOT NULL,
  `addrss` longtext NOT NULL

Now create a database connection

define('DB_PASS' ,'');
define('DB_NAME', 'test');
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
// Check connection
if (mysqli_connect_errno())
 echo "Failed to connect to MySQL: " . mysqli_connect_error();

Create a stored Procedure with name insertuser.

(IN name VARCHAR(255), IN email VARCHAR(255), IN contactno BIGINT, IN addrss LONGTEXT)
 insert into user(name,email,contactno,addrss) VALUES(name,email,contactno,addrss)$

Argument Modes
IN : Data Values comes in form the calling process and is not changed.
OUT : No Data Value comes in form the calling process; on normal exit, value of argument is passed back to caller.
IN OUT : Data Values comes in form the calling process, and another value is returned on normal exit.
Now create a HTML form (Index.php)

<form name="stmt" method="post">
<td>Name :</td>
<td><input type="text" name="name" required="required" /> </td>
<td>Email :</td>
<td><input type="email" name="email" required="required" /></td>
<td>Contact no. :</td>
<td><input type="text" name="contact" required="required" /></td>
<td>Address :</td>
<td><textarea name="addrss" cols="30" rows="4" required="required"></textarea></td>
<td><input type="submit" name="submit" value="Submit" /></td>

Now execute the store Procedure. Put this on the top of the index Page

// Code for execute the Store Procedure
$sql=mysqli_query($con,"CALL insertuser('$name','$email','$contact','$addrss')");
echo "<script>alert('Data Inserted');</script>";
echo "<script>alert('not inserted');</script>";

How to run demo on localhost
First, download the zip package.
Extract  the zip file  and paste the file in root directory.
Now create a database name with test and import the SQL file available inside the package.
Create a store procedure available inside the package copy .

Download Source Code(Store Procedure In PHP)
Size: 1.84 KB
Version: V 1.0

Leave A Reply

Your email address will not be published.

1 Comment
  1. […] previous tutorial, we learned about stored procedure in PHP. In this tutorial, we will learn CRURD operation using Stored […]

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy