Call stored procedure mysql in php

  1. The main problem with stored procedures
  2. Comments

Calling a stored procedure in mysqli is helluva tricky business. There are bugs and limitations to avoid as well as many tricks to employ.

Well, to be honest, in the simplest case possible (when a stored procedure is the only sql query your code has to run, and the procedure itself is relatively simple, not using cursors or returning multiple result sets) you can call it just as any other query.

But the more complex your stored procedure gets, the more complex the calling code would be.

There is one thing about stored procedures any programmer stumbles upon at first: every stored procedure always returns one extra result set: one (or many) results with actual data, and one just empty. Which means if you try to call a procedure and then proceed to another query, then "Cannot execute queries while other unbuffered queries are active" error will occur, because you have to clear out that extra empty result first.

Therefore, in order to proceed we've got to winkle out that extra result set from mysql first. And mysqli::next_result does exactly that. So basically we would only need to call this function in order to let our next SQL queries work.

Also, we will need this function

Calling a stored procedure without parameters that returns a single result set

As it was said above, in case your procedure is the only database interaction on the page, you can call it right away, without any additional stuff. But usually it is not the case and you have to execute other queries after the procedure. Hence, we've got to take out that extra result set from our way.

When our query doesn't take any parameters and returns just one result set, we could add just a single call to mysqli::next_result

$result $mysqli->query("call mysqli_call()");
echo 
json_encode($result->fetch_all(MYSQLI_ASSOC)), "\n";
$mysqli->next_result();
// here we go - the next query works!
$next_query $mysqli->query("SELECT 'next' as `from` from dual");
echo 
json_encode($next_query->fetch_assoc()), "\n";

Calling a stored procedure without parameters that returns multiple result sets


  • Mysqli SELECT query with prepared statements
  • How to run an INSERT query using Mysqli
  • How to run a SELECT query using Mysqli
  • How to run an UPDATE query using Mysqli
  • Mysqli prepared statement with multiple values for IN clause
  • Using mysqli prepared statements with LIKE operator in SQL
  • Mysqli examples
  • How to create a search filter for mysqli
  • How to run 1000s insert queries with mysqli?

Summary: in this tutorial, you will learn how to call a MySQL stored procedure using the PHP PDO.

Setting up a stored procedure in MySQL

To execute a statement in the MySQL database, you can use any MySQL client tool e.g., mysql client tool or MySQL Workbench.

First, insert data into the authors table by executing the following INSERT statement:

INSERT INTO books(title, isbn, published_date,publisher_id) VALUES ('Goodbye to All That','9781541619883','2013-01-05', 3), ('The Mercies','9780316529235','2020-01-28', 3), ('On the Farm','9780763655914','2012-03-27', 2), ('Joseph Had a Little Overcoat','9780140563580','1977-03-15', 2);

Code language: SQL (Structured Query Language) (sql)

Note that the publishers table should have rows with id 2 and 3. If it doesn’t, you can run the script that inserts rows into the publishers table.

Second, execute the following CREATE PROCEDURE statement to create a new stored procedure called get_books_published_after:

USE `bookdb`; DELIMITER $$ USE `bookdb`$$ CREATE PROCEDURE `get_books_published_after` (IN published_year INT) BEGIN SELECT book_id, title, isbn, published_date, name as publisher FROM books b INNER JOIN publishers p ON p.publisher_id = b.publisher_id WHERE year(published_date) > published_year; END$$ DELIMITER ;

Code language: SQL (Structured Query Language) (sql)

The stored procedure get_books_published_after returns all books published after a specific year.

Third, execute the stored procedure to check the result set:

CALL get_books_published_after(2010);

Code language: SQL (Structured Query Language) (sql)

The statement returns the following result set:

+---------+---------------------+---------------+----------------+----------------------+ | book_id | title | isbn | published_date | publisher | +---------+---------------------+---------------+----------------+----------------------+ | 1 | Goodbye to All That | 9781541619883 | 2013-01-05 | Hachette Book Group | | 2 | The Mercies | 9780316529235 | 2020-01-28 | Hachette Book Group | | 3 | On the Farm | 9780763655914 | 2012-03-27 | Penguin/Random House | +---------+---------------------+---------------+----------------+----------------------+ 3 rows in set (0.005 sec)

Code language: plaintext (plaintext)

Calling a MySQL stored procedure from PHP using PDO

The following script illustrates how to call the get_books_published_after stored procedure:

<?php $published_year = 2010; // connect to the database and select the publisher $pdo = require 'connect.php'; $sql = 'CALL get_books_published_after(:published_year)'; $publishers = []; $statement = $pdo->prepare($sql); $statement->bindParam(':published_year', $published_year, PDO::PARAM_INT); $statement->execute(); $publishers = $statement->fetchAll(PDO::FETCH_ASSOC); print_r($publishers);

Code language: HTML, XML (xml)

How it works.

First, create a new connection to the MySQL database:

$pdo = require 'connect.php';

Code language: PHP (php)

Second, construct a SQL statement that calls the get_books_published_after stored procedure:

$sql = 'CALL get_books_published_after(:published_year)';

Code language: PHP (php)

The statement accepts a named placeholder :published_year so that you can bind a value to it later.

Third, create a prepared statement by calling the prepare() method of the PDO instance:

$statement = $pdo->prepare($sql);

Code language: PHP (php)

Fourth, bind a value to the statement:

$statement->bindParam(':published_year', $published_year, PDO::PARAM_INT);

Code language: PHP (php)

Fifth, execute the stored procedure call:

$statement->execute();

Code language: PHP (php)

Since the stored procedure returns a result set, you can fetch each row in the result set into an associative array using the fetchAll() method:

$publishers = $statement->fetchAll(PDO::FETCH_ASSOC);

Code language: PHP (php)

Summary

  • Use a prepared statement to call a MySQL stored procedure from PHP.

Did you find this tutorial useful?

How do I call a PHP stored procedure with parameters in MySQL?

How to Call a Stored Procedure From PHP in MySQL.
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..
Add the PHP connection to the MySQL database. ... .
Select a database name. ... .
Call the stored procedure to retrieve MySQL records..

How can we call stored procedure with parameters in PHP with SQL Server?

To call a stored procedure from a PHP application, you prepare and execute an SQL CALL statement. The procedure that you call can include input parameters (IN), output parameters (OUT), and input and output parameters (INOUT).

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.

How do I call a procedure with parameters in MySQL?

This procedure accepts id of the customer as IN parameter and returns product name (String), customer name (String) and, price (int) values as OUT parameters from the sales table. To call the procedure with parameters pass @parameter_name as parameters, in these parameters the output values are stored.