Jump to content

Stored Procedure output via PDO


joemangrove

Recommended Posts

Hi,

 

I am trying to get the output of a stored procedure via PDO.  I am currently using the latest versions of PHP and Mysql. 

 

Stored Procedure:

DELIMITER $$

DROP PROCEDURE IF EXISTS `database`.`example_sproc` $$
CREATE DEFINER=`user`@`ip` PROCEDURE `example_sproc`(IN size_variable VARCHAR(20), OUT out_variable VARCHAR(20))
BEGIN
SELECT size FROM room WHERE size=size_variable;
END $$

DELIMITER ;

 

PHP:

<?php
$size_variable = "very big";
$username="test";
$password="1234567";

$dsn = 'mysql:dbname=database;host=hostname;port=3306';
try {
  $dbh = new PDO($dsn, $user, $password);
}
catch (PDOException $e) {
  die('Connection failed: '.$e->getMessage(  ));
}
$sql="CALL example_sproc(?, @output_variable)";
$sth = $dbh->prepare($sql)  or die(implode(':', $sth->errorInfo(  )));
$sth = bindparam(1, $size_variable, PDO::PARAM_STR, 4000);
$sth->execute() or die (implode(':', $sth->errorInfo()));
$sql="SELECT @out_variable";
foreach ($dbh->query($sql) as $row) {
  printf("%s\n", $row[0]);
}
?>

 

The above code does not print out the desired output and does not print a php or mysql error.  However, when using mysqli the stored procedure outputs the variable just fine, so the stored procedure shoud be ok. 

 

This is driving me crazy; any help would be greatly appreciated.

 

Sincerely,

Ryan

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.