2

I need to retrieve the OUT Parameters from a MySQL Stored Procedure. I can't find anything that explains this (and makes sense to me).

try {
$dsn = 'mysql:dbname=db_name;host=localhost';
$dbh = new PDO($dsn, 'usr_name', 'password');
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}

$stmt = $dbh->prepare("CALL db.stprNewUser(:usrEmail,:newUserOK,:stprComment)");

$stmt->bindParam(':usrEmail', $tmpEmail, PDO::PARAM_STR); 
$stmt->bindParam(':newUserOK', $newUserOK, PDO::PARAM_INT,1); 
$stmt->bindParam(':stprComment', $stprComment, PDO::PARAM_STR,100); 

$stmt->execute();

$outputArray = $dbh->query("select @newUserOK, @stprComment")->fetch(PDO::FETCH_ASSOC);

print "procedure returned [" . $outputArray['@newUserOK'] . $outputArray['@stprComment'] . "]\n";

I found the last two lines on another SO item, but it just returns NULL values.

0

3 Answers 3

4

try this... see if works...

try 
{
    $dsn = 'mysql:dbname=db_name;host=localhost';
    $dbh = new PDO($dsn, 'usr_name', 'password');
} 
catch (PDOException $e) 
{
    echo 'Connection failed: ' . $e->getMessage();
}


//$stmt = $dbh->prepare("CALL db.stprNewUser(:usrEmail,:newUserOK,:stprComment)");
//changed :newUserOK to @newUserOK
//changed :stprComment to @stprComment
$stmt = $dbh->prepare("CALL db.stprNewUser(:usrEmail,@newUserOK,@stprComment);");

//declare only input parameters.
//good pratice put string length. assuming varchar(100).
$stmt->bindParam(':usrEmail', $tmpEmail, PDO::PARAM_STR,100); 

//dont need these
// $stmt->bindParam(':newUserOK', $newUserOK, PDO::PARAM_INT,1); 
// $stmt->bindParam(':stprComment', $stprComment, PDO::PARAM_STR,100); 

$stmt->execute();

$outputArray = $dbh->query("select @newUserOK, @stprComment;")->fetchAll();

foreach($outputArray as $row)
{
   "NewUserOk:" .  $row["@newUserOk"] . ", StprComment:" . $row["@stprComment"];
}

//$outputArray = $dbh->query("select @newUserOK, @stprComment")->fetch(PDO::FETCH_ASSOC);
//print "procedure returned [" . $outputArray['@newUserOK'] . $outputArray['@stprComment'] . "]\n";
Sign up to request clarification or add additional context in comments.

Comments

1

Apart from using MySQL session variables, you can just use bindParam():

bool PDOStatement::bindParam ( mixed $parameter , mixed &$variable [, int $data_type = PDO::PARAM_STR [, int $length [, mixed $driver_options ]]] )

Binds a PHP variable to a corresponding named or question mark placeholder in the SQL statement that was used to prepare the statement. Unlike PDOStatement::bindValue(), the variable is bound as a reference and will only be evaluated at the time that PDOStatement::execute() is called.

Most parameters are input parameters, that is, parameters that are used in a read-only fashion to build up the query. Some drivers support the invocation of stored procedures that return data as output parameters, and some also as input/output parameters that both send in data and are updated to receive it.

Don't forget to use the corresponding placeholders:

$stmt = $dbh->prepare("CALL superior_main_db.stprNewUser(:usrEmail, :newUserOK, :stprComment)");

Once you execute the statement, your variables will automatically contain the values you need.

5 Comments

The OP is asking how to use stored procedures, not how to bind parameters isn't he?
@vascowhite - Please read the question again. He's asking how to retrieve OUT parameters.
Oh yes, you're right. He's already said he doesn't understand the manual though, so this isn't going to help without an example.
@vascowhite - I've made a little edit, let's see if its clearer now.
Still not getting the results of the output parameters. :-s
0

To retrieve @newUserOK and @stprComment variables, just execute below query after calling stored procedure as below

SELECT @newUserOK, @stprComment

2 Comments

Amit, how do I execute the above query in the context of my current code?
Same as you execute other queries $stmt = $dbh->prepare ("SELECT @newUserOK, @stprComment");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.