BIG PDO issues with StoredProc output params with PHP 5 and MySQL5

This is a discussion on BIG PDO issues with StoredProc output params with PHP 5 and MySQL5 within the PHP Language forums, part of the PHP Programming Forums category; Hey everyone... I'm having an issue with a seemingly simple piece of PHP/MySQL I have a stored procedure ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-10-2006
James
 
Posts: n/a
Default BIG PDO issues with StoredProc output params with PHP 5 and MySQL5

Hey everyone...
I'm having an issue with a seemingly simple piece of PHP/MySQL

I have a stored procedure in MySQL5 as such:

SQL:
--------------
DELIMITER $$;
DROP PROCEDURE IF EXISTS `test`.`sp_returns_string`$$
CREATE PROCEDURE `test`.`sp_returns_string`(OUT vOutput varchar(32))
BEGIN
SET vOutput = 'It Worked';
END$$
DELIMITER ;$$
--------------

And a piece of PHP that uses PDO to call the Stored Proc as such:

PHP:
--------------
<?php

//First - check/load PDO!
if (!extension_loaded('pdo_mysql')) {
// If not loaded we could try loading it manually
$prefix = (PHP_SHLIB_SUFFIX == 'dll') ? 'php_' : '';
if (!@dl($prefix . 'pdo_mysql.' . PHP_SHLIB_SUFFIX)) {
die('pdo_mysql unavailable');
}
}

$DB_host = "localhost"; // the hostname of the database server
$DB_user = "root"; // YOUR username to connect with
$DB_pass = "password"; // YOUR user's password
$DB_dbName = "test"; // the name of the database to connect to
$DB_dbType = "mysqli"; // the type of database server.

$DB_Con = "mysql:host=$DB_host;dbname=$DB_dbName";
$dbOptions = array();

//Create a DB connection
$db = new PDO($DB_Con, $DB_user, $DB_pass, $dbOptions);

$return_value = '';
$stmt = $db->prepare("CALL sp_returns_string(@?)");
$stmt->bindParam(1, $return_value, PDO::PARAM_STR, 4000);
$stmt->execute();
print_r($stmt);
print "<br/>Returned: $return_value<br/><br/>\r\n";
?>
--------------

This should print something like this:
--------------
PDOStatement Object ( [queryString] => CALL sp_returns_string(@?) )
Returned: It Worked!
--------------

But unfortunately it NEVER returns anything.
The Stored Proc works fine and returns the string ok, but only when I'm
calling it from an SQL console.

The PHP PDO documentation says to use:
--------------
$return_value = '';
$stmt = $db->prepare("CALL sp_returns_string(?)");
$stmt->bindParam(1, $return_value, PDO::PARAM_STR, 4000);
--------------
Note the missing "@".
But when I do this, it complains that the parameter isn't a variable and
the call dies.

(see: http://www.php.net/pdo/ for the examples)

Any help would be great!

Thanks.
James.
Reply With Quote
  #2 (permalink)  
Old 01-10-2006
Louis-Philippe Huberdeau
 
Posts: n/a
Default Re: BIG PDO issues with StoredProc output params with PHP 5 and MySQL 5

I have always used named parameters with PDO. I don't like having to rely on
the parameter order.

$stmt = $db->prepare( "SELECT foo FROM bar WHERE baz = :baz" );
$stmt->bindParam( ':baz', 123 );
$stmt->execute();

Did you try using PDO with a simple query first using your @? symbol? The
problem might not be with the stored procedure, but with the parameter
binding.

--
Louis-Philippe Huberdeau

James wrote:

> Hey everyone...
> I'm having an issue with a seemingly simple piece of PHP/MySQL
>
> I have a stored procedure in MySQL5 as such:
>
> SQL:
> --------------
> DELIMITER $$;
> DROP PROCEDURE IF EXISTS `test`.`sp_returns_string`$$
> CREATE PROCEDURE `test`.`sp_returns_string`(OUT vOutput varchar(32))
> BEGIN
> SET vOutput = 'It Worked';
> END$$
> DELIMITER ;$$
> --------------
>
> And a piece of PHP that uses PDO to call the Stored Proc as such:
>
> PHP:
> --------------
> <?php
>
> //First - check/load PDO!
> if (!extension_loaded('pdo_mysql')) {
> // If not loaded we could try loading it manually
> $prefix = (PHP_SHLIB_SUFFIX == 'dll') ? 'php_' : '';
> if (!@dl($prefix . 'pdo_mysql.' . PHP_SHLIB_SUFFIX)) {
> die('pdo_mysql unavailable');
> }
> }
>
> $DB_host = "localhost"; // the hostname of the database server
> $DB_user = "root"; // YOUR username to connect with
> $DB_pass = "password"; // YOUR user's password
> $DB_dbName = "test"; // the name of the database to connect to
> $DB_dbType = "mysqli"; // the type of database server.
>
> $DB_Con = "mysql:host=$DB_host;dbname=$DB_dbName";
> $dbOptions = array();
>
> //Create a DB connection
> $db = new PDO($DB_Con, $DB_user, $DB_pass, $dbOptions);
>
> $return_value = '';
> $stmt = $db->prepare("CALL sp_returns_string(@?)");
> $stmt->bindParam(1, $return_value, PDO::PARAM_STR, 4000);
> $stmt->execute();
> print_r($stmt);
> print "<br/>Returned: $return_value<br/><br/>\r\n";
> ?>
> --------------
>
> This should print something like this:
> --------------
> PDOStatement Object ( [queryString] => CALL sp_returns_string(@?) )
> Returned: It Worked!
> --------------
>
> But unfortunately it NEVER returns anything.
> The Stored Proc works fine and returns the string ok, but only when I'm
> calling it from an SQL console.
>
> The PHP PDO documentation says to use:
> --------------
> $return_value = '';
> $stmt = $db->prepare("CALL sp_returns_string(?)");
> $stmt->bindParam(1, $return_value, PDO::PARAM_STR, 4000);
> --------------
> Note the missing "@".
> But when I do this, it complains that the parameter isn't a variable and
> the call dies.
>
> (see: http://www.php.net/pdo/ for the examples)
>
> Any help would be great!
>
> Thanks.
> James.


Reply With Quote
  #3 (permalink)  
Old 01-11-2006
James
 
Posts: n/a
Default Re: BIG PDO issues with StoredProc output params with PHP 5 and MySQL5

Hey,
Thanks for the feedback.

Unfortunately using param names doesn't work either.
All Database functions seem to work perfectly "except" fro output params.
All of the examples I've found on the net point to using "?" as the
parameter rather than "@?" (or "@:name" rather than ":name" for var names)
Unfortunately when I do this, I get :
> SQLSTATE: HY000
> ERROR CODE: 1414
> ERROR: OUT or INOUT argument 1 for routine test.sp_returns_string is

not a variable

Anyway, when I include the "@" it simply returns nothing...

I just can't seem to find any working examples anywhere...

Thanks,
James.

Louis-Philippe Huberdeau wrote:
> I have always used named parameters with PDO. I don't like having to rely on
> the parameter order.
>
> $stmt = $db->prepare( "SELECT foo FROM bar WHERE baz = :baz" );
> $stmt->bindParam( ':baz', 123 );
> $stmt->execute();
>
> Did you try using PDO with a simple query first using your @? symbol? The
> problem might not be with the stored procedure, but with the parameter
> binding.
>
> --
> Louis-Philippe Huberdeau
>

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:23 PM.


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