Take the 2-minute tour ×
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It's 100% free, no registration required.

I have a stored procedure, called "myprocedure". How can I call it from Magento?

I tried:

$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = 'CALL myprocedure()';
$results = $readConnection->fetchAll($query);
var_dump($results);

This gives me a PDO error, whats wrong?

Thanks!

share|improve this question
    
this might lead you in the right direction: stackoverflow.com/questions/6282391/… –  pzirkind Jun 16 at 13:48
    
Thanks, but this is not for calling it, but for the creation. I need to actually call it from an observer. –  user1638055 Jun 16 at 13:49
1  
what if you use $readConnection->exec($query) instead of fetchAll –  pzirkind Jun 16 at 13:58
    
Yeah, that did the trick, big thanks! –  user1638055 Jun 16 at 14:19
    
@pzirkind. Please write you comment as an answer so the OP can accept it. –  Marius Jun 16 at 14:26

2 Answers 2

Try using prepare and execute instead:

$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = 'CALL myprocedure()';
$sql   = $readConnection->prepare($query);
$sql->execute();
$data  = $sql->fetchAll();
share|improve this answer

It seems that to execute stored procedures you would need to use the following code:

$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = 'CALL myprocedure()';
$results = $readConnection->exec($query); // this is the line that needs changing (use exec)

In other words use exec() instead of fetchAll()

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.