Why we use stored procedure in php?

This post will cover how to create Stored Procedures using PHP My Admin. Next part will cover how to execute Stored Procedures using Laravel.

First of all what is Stored Procedure?

A stored procedure is a prepared SQL code that you can save, so the code can be reused over and over again.

You can pass parameters to the stored procedure to get data based on Dynamic values.

Step -1 : Open PHP My Admin and select the database to create stored procedure

Step -2 : Go to Routines menu & Click on Add routine.

Why we use stored procedure in php?

Step -3 : By Clicking on Add Routine Link, PHPMyAdmin will open Pop-up.

Why we use stored procedure in php?

Step -4 : Follow the steps and create stored procedure.Create stored procedure to get data from users table( Without Parameters) .

Stored procedure Without Parameters

Why we use stored procedure in php?

Without Parameters

CREATE PROCEDURE `GerUsers`() NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER BEGIN Select * From users; END

Stored Procedure with Parameters

Why we use stored procedure in php?

With Parameters

CREATE PROCEDURE `GetUserByEmail`(IN `uEmail` VARCHAR(255)) NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER BEGIN select * from users where email = uEmail; END

Execute Stored Procedure from PHP MyAdmin

PHP MyAdmin will display list of created Stored Procedures.

Why we use stored procedure in php?

List of Stored Procedure

Click on Execute link to Run Specific Stored Procedure.

Procedure without parameters will directly Run Query and list out the data Procedure parameters will open Pop up to add parameters, then Run Procedure and get result data.

That’s it. Hope this article helps you guys to understand how to mange Stored Procedure Using PHP My Admin. In the Next article I will explain how to Execute Stored Procedure In Laravel Framework.

Part 2 : https://medium.com/@nishitvmaheta/how-to-execute-stored-procedure-using-laravel-8d067c306154

Stored procedures provide PHP programmers with pre-programmed database applications that retrieve and edit MySQL records. Stored procedures let you send parameters to the database program, so you can retrieve only a specific set of records. You must connect to the database and use the "mysql_query" function to call a MySQL stored procedure from a PHP page.

  1. Right-click the PHP page you want to use to call the stored procedure and select "Open With." Click the PHP editor to open the code.

  2. Add the PHP connection to the MySQL database. The following code connects to a database server named "dbserver" with a username and password:

    $server= mysql_connect(‘dbserver’, ‘username’, ‘password’);

    Replace the three values with your own database name and username and password.

  3. Select a database name. The following code activates your database name, which contains the stored procedure:

    $db = mysql_select_db(‘mydatabase’, $server);

    Change the "mydatabase" text to your own database name.

  4. Call the stored procedure to retrieve MySQL records. The following code calls a stored procedure and returns a record set of customers:

    $records = mysql_query( ‘CALL get_customers()’ );

    Replace "get_customers" with your own stored procedure.

SQL Stored Procedures for SQL Server


What is a Stored Procedure?

A stored procedure is a prepared SQL code that you can save, so the code can be reused over and over again.

So if you have an SQL query that you write over and over again, save it as a stored procedure, and then just call it to execute it.

You can also pass parameters to a stored procedure, so that the stored procedure can act based on the parameter value(s) that is passed.

Stored Procedure Syntax

CREATE PROCEDURE procedure_name
AS
sql_statement
GO;

Execute a Stored Procedure


Demo Database

Below is a selection from the "Customers" table in the Northwind sample database:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
5 Berglunds snabbköp Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden


Stored Procedure Example

The following SQL statement creates a stored procedure named "SelectAllCustomers" that selects all records from the "Customers" table:

Example

CREATE PROCEDURE SelectAllCustomers
AS
SELECT * FROM Customers
GO;

Execute the stored procedure above as follows:

Example

EXEC SelectAllCustomers;



Stored Procedure With One Parameter

The following SQL statement creates a stored procedure that selects Customers from a particular City from the "Customers" table:

Example

CREATE PROCEDURE SelectAllCustomers @City nvarchar(30)
AS
SELECT * FROM Customers WHERE City = @City
GO;

Execute the stored procedure above as follows:

Example

EXEC SelectAllCustomers @City = 'London';


Stored Procedure With Multiple Parameters

Setting up multiple parameters is very easy. Just list each parameter and the data type separated by a comma as shown below.

The following SQL statement creates a stored procedure that selects Customers from a particular City with a particular PostalCode from the "Customers" table:

Example

CREATE PROCEDURE SelectAllCustomers @City nvarchar(30), @PostalCode nvarchar(10)
AS
SELECT * FROM Customers WHERE City = @City AND PostalCode = @PostalCode
GO;

Execute the stored procedure above as follows:

Example

EXEC SelectAllCustomers @City = 'London', @PostalCode = 'WA1 1DP';



Why we use stored procedure?

A stored procedure provides an important layer of security between the user interface and the database. It supports security through data access controls because end users may enter or change data, but do not write procedures.

What is stored procedure in PHP?

A stored procedure is a subroutine stored in the database catalog. Applications can call and execute the stored procedure. The CALL SQL statement is used to execute a stored procedure. Stored procedures can have IN , INOUT and OUT parameters, depending on the MySQL version.

Why we use stored procedure instead of query?

A stored procedure is invoked as a function call instead of a SQL query. Stored procedures can have parameters for both passing values into the procedure and returning values from the call. Results can be returned as a result set, or as an OUT parameter cursor.