This topic is locked

How to call a MySQL stored procedure with output parameters

11/25/2013 10:31:07 PM
PHPRunner Tips and Tricks
admin

In this article we'll figure out how to call MySQL stored procedures from PHPRunner events.
Here is our stored procedure that accepts two parameters, adds them up and returns the result in OUT parameter total.

DELIMITER $$

CREATE PROCEDURE mysum(IN par1 decimal(20,2),

IN par2 decimal(20,2),

OUT total decimal(20,2))

BEGIN

SET total = par1 + par2;

END$$

DELIMITER ;


You can execute the following via phpMyAdmin or Navicat to make sure your stored procedure works:



call mysum(10.5,25,@total);

select @total as t;


It will return the value of 35.50. Now lets see how we can call this stored procedure from PHPRunner event like BeforeAdd. In the example we add up two fields and save the result in another field.

CustomQuery("call mysum(".$values["field1"].",".$values["field2"].",@total);");

$rs = CustomQuery("select @total;");

$data = db_fetch_array($rs);

$values["field3"]=$data["@total"];


First SQL query makes stored procedure call, sending values of field1 and field2 as parameters. Second SQL query reads the output variable.

A
Anapolis 2/20/2014



In this article we'll figure out how to call MySQL stored procedures from PHPRunner events.
Here is our stored procedure that accepts two parameters, adds them up and returns the result in OUT parameter total.

DELIMITER $$

CREATE PROCEDURE mysum(IN par1 decimal(20,2),

IN par2 decimal(20,2),

OUT total decimal(20,2))

BEGIN

SET total = par1 + par2;

END$$

DELIMITER ;


You can execute the following via phpMyAdmin or Navicat to make sure your stored procedure works:



call mysum(10.5,25,@total);

select @total as t;


It will return the value of 35.50. Now lets see how we can call this stored procedure from PHPRunner event like BeforeAdd. In the example we add up two fields and save the result in another field.

CustomQuery("call mysum(".$values["field1"].",".$values["field2"].",@total);");

$rs = CustomQuery("select @total;");

$data = db_fetch_array($rs);

$values["field3"]=$data["@total"];


First SQL query makes stored procedure call, sending values of field1 and field2 as parameters. Second SQL query reads the output variable.


Brilliant. From my test I see that this is saved "at the top", that is , right into the database if I have Admin rights.
So procedures are callable in a PHPRunner project regardless of whether a table that is involved is type INNODB or myISAM?
And can all procedures be exported from one database and imported to another?