Prepare the statement that calls the stored procedure for execution using the prepare() method of the PDO object. The prepare() method returns a PDOStatement object.
Optionally pass values to the statement using the bindValue() method.
Execute the statement using the execute() method of the PDOStatement object. You can pass the values to the statement when calling the execute() method as well.
Get the value using the fetchColumn() method that returns a single column of the next row in the result set.
The following add() method demonstrates how to call the add() stored procedure in PostgreSQL database.
To test the add() method, you use the following code in the index.php file:
Calling a stored procedure that returns a result set
We will use the accounts, plans, and account_plans tables for the sake of demonstration. The following get_accounts() stored procedure returns a result set that contains complete data of accounts.
The steps of calling a stored procedure that returns a result set are the same as the steps of querying data.
The following getAccounts() method demonstrates how to call the get_accounts() stored procedure in PHP.
To test the getAccounts() method, you use the following code in the account.php file.
In this tutorial, we have shown you how to call stored procedures from PostgreSQL using PHP PDO.