How to get stored procedure return values via PDO?

This is a discussion on How to get stored procedure return values via PDO? within the PHP General forums, part of the PHP Programming Forums category; Hi, I'm calling a MySQL Stored Procedure via PDO (PHP 5.2.3) $stmt = $dbh->prepare('CALL forum_post(?, ?, ?, ?, ?, @...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-26-2007
Richard Davey
 
Posts: n/a
Default How to get stored procedure return values via PDO?

Hi,

I'm calling a MySQL Stored Procedure via PDO (PHP 5.2.3)

$stmt = $dbh->prepare('CALL forum_post(?, ?, ?, ?, ?, @status, @thread_id, @message_id)');

At the moment in order to get the values of status, thread_id and
message_id I need to issue a second query:

$sql = "SELECT @status AS status, @thread_id AS thread_id, @message_id AS message_id";

and then foreach my way through it:

foreach ($dbh->query($sql) as $row)
{
$status = $row['status'];
$thread_id = $row['thread_id'];
$message_id = $row['message_id'];
}

Which seems a bit insane.. is there no way to do a bindValue at the
same time as I do my bindParams?

Now I write this I really can't remember why I am even putting that
second query into a foreach loop, even so it's still a step I'd like
to remove entirely if possible?

Cheers,

Rich
--
Zend Certified Engineer
http://www.corephp.co.uk

"Never trust a computer you can't throw out of a window"
Reply With Quote
  #2 (permalink)  
Old 07-26-2007
M. Sokolewicz
 
Posts: n/a
Default Re: How to get stored procedure return values via PDO?

Richard Davey wrote:
> Hi,
>
> I'm calling a MySQL Stored Procedure via PDO (PHP 5.2.3)
>
> $stmt = $dbh->prepare('CALL forum_post(?, ?, ?, ?, ?, @status, @thread_id, @message_id)');
>
> At the moment in order to get the values of status, thread_id and
> message_id I need to issue a second query:
>
> $sql = "SELECT @status AS status, @thread_id AS thread_id, @message_id AS message_id";
>
> and then foreach my way through it:
>
> foreach ($dbh->query($sql) as $row)
> {
> $status = $row['status'];
> $thread_id = $row['thread_id'];
> $message_id = $row['message_id'];
> }
>
> Which seems a bit insane.. is there no way to do a bindValue at the
> same time as I do my bindParams?
>
> Now I write this I really can't remember why I am even putting that
> second query into a foreach loop, even so it's still a step I'd like
> to remove entirely if possible?
>
> Cheers,
>
> Rich


From: http://www.php.net/manual/en/ref.pdo.php

Example 1716. Calling a stored procedure with an input/output parameter
<?php
$stmt = $dbh->prepare("CALL sp_takes_string_returns_string(?)");
$value = 'hello';
$stmt->bindParam(1, $value, PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT, 4000);

// call the stored procedure
$stmt->execute();

print "procedure returned $value\n";
?>
Reply With Quote
  #3 (permalink)  
Old 07-26-2007
Richard Davey
 
Posts: n/a
Default Re: [PHP] Re: How to get stored procedure return values via PDO?

Hi M.,

Thursday, July 26, 2007, 2:09:47 PM, you wrote:

> Richard Davey wrote:
>> Hi,
>>
>> I'm calling a MySQL Stored Procedure via PDO (PHP 5.2.3)
>>
>> $stmt = $dbh->prepare('CALL forum_post(?, ?, ?, ?, ?, @status, @thread_id, @message_id)');
>>
>> At the moment in order to get the values of status, thread_id and
>> message_id I need to issue a second query:
>>
>> $sql = "SELECT @status AS status, @thread_id AS thread_id, @message_id AS message_id";
>>
>> and then foreach my way through it:
>>
>> foreach ($dbh->query($sql) as $row)
>> {
>> $status = $row['status'];
>> $thread_id = $row['thread_id'];
>> $message_id = $row['message_id'];
>> }
>>
>> Which seems a bit insane.. is there no way to do a bindValue at the
>> same time as I do my bindParams?
>>
>> Now I write this I really can't remember why I am even putting that
>> second query into a foreach loop, even so it's still a step I'd like
>> to remove entirely if possible?
>>
>> Cheers,
>>
>> Rich


> From: http://www.php.net/manual/en/ref.pdo.php


> Example 1716. Calling a stored procedure with an input/output parameter


It's not an INOUT parameter though.

Cheers,

Rich
--
Zend Certified Engineer
http://www.corephp.co.uk

"Never trust a computer you can't throw out of a window"
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:08 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0