MySQL tárolt eljárás

, ,

forrás: http://www.sitepoint.com/stored-procedures-mysql-php/
Ha a webszerveren megfelelő jogosultságunk van.

CREATE TABLE `salary` (
`empid` int(11) NOT NULL,
`sal` int(11) DEFAULT NULL,
PRIMARY KEY (`empid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE USER ‘tr’@’localhost’ IDENTIFIED BY ‘mypass’;

grant execute on hris.*  to tr@`%`;

DELIMITER $$

CREATE PROCEDURE `avg_sal`(out avg_sal decimal)
BEGIN
select avg(sal) into avg_sal from salary;

END

call avg_sal(@out);
select @out;

Hívás PHP-ból:

$dbms = ‘mysql’;

//Replace the below connection parameters to fit your environment
$host = ‘192.168.1.8’;
$db = ‘hris’;
$user = ‘tr’;
$pass = ‘mypass’;
$dsn = “$dbms:host=$host;dbname=$db”;

$cn=new PDO($dsn, $user, $pass);

$q=$cn->exec(‘call avg_sal(@out)’);
$res=$cn->query(‘select @out’)->fetchAll();
print_r($res);

Hívás Zend Framework-ből:

$db = Zend_Registry::get('db');
$stmt = $db->query("CALL sp_distinctlicence()");

$data = $stmt->fetchAll();
Zend_Debug::dump($data,$label=null, $echo=false);

http://stackoverflow.com/questions/2325363/how-to-call-stored-procedure-on-zend-framework

Blogbook : PHP | Javascript | Laravel | Corcel | CodeIgniter | VueJs | ReactJs | WordPress