Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary output parameters - PHP, PDO, MySQL: result is always null

Tags:

php

mysql

pdo

I have a MySQL stored proc that returns a UUID via an output parameter. I'm calling this proc from PHP using PDO - but I'm just getting null back for the output param... There's no explicit binary option for the output data type (PDO::PARAM_???) - the closest I guess is LOB. Does anyone know how to do this?Here's my PHP code:

public function CreateTenant($name)
{
    $qry = $this->conn->prepare("call spInsertTenant(:name, :userId, :id)");
    $qry->bindParam(':name', $name);
    $qry->bindParam(':userId', $this->currUser);
    $qry->bindParam(':id', $id, PDO::PARAM_LOB, 16);
    try {
        $qry->execute();
        return $id;
    } catch (PDOException $e) {
        echo $e->getMessage();
        exit;
    }
}

EDIT: Ok - the original proc is pretty long. Here's a combination of MySQL and PHP I knocked up that is having the same problem. Note - there's nothing inserted into my table - even though no errors are thrown. Thanks Mihai for the nod towards declaring the $vars first... Tried that without any luck. Also trincot - my UUIDs are definitely binary - they're using this: https://www.percona.com/blog/2014/12/19/store-uuid-optimized-way/

Here's the stored proc:

CREATE TABLE IF NOT EXISTS `Tenant2` (
  `ID` BINARY(16) NOT NULL,
  `Name` VARCHAR(45) NULL,
  PRIMARY KEY (`ID`));

drop procedure if exists spInsertTenant2;
delimiter $$
CREATE PROCEDURE `spInsertTenant2`(pName nvarchar(45), out oId binary(16))
begin

    set @id = ordered_uuid(uuid());

    insert  Tenant2 (ID, Name)
    values  (@id, pName);

    set oId = @id;

end$$
delimiter ;

call spInsertTenant2('Test', @id);
select * from Tenant2;
select @id, hex(@id);

And here's some PHP to hook into this:

...
// Create connection
$this->conn = new PDO('mysql:dbname=' . $this->database . ';host=' . $this->servername, $this->username, $this->password);
...

function testParam()
{
    try {
        $name = 'SomeName';
        $id = 0;
        $qry = $this->conn->prepare('call spInsertTenant2(:name :id)');
        $qry->bindParam(':name', $name);
        $qry->bindParam(':id', $id, PDO::PARAM_INT | PDO::PARAM_INPUT_OUTPUT);
        $qry->execute();
        print $id;
    } catch (PDOException $e) {
        echo $e->getMessage();
        exit;
    }
}

Note the connection to the DB is working - I'm able to execute select statements and get data back no probs.

like image 817
Matt Wales Avatar asked Feb 13 '26 02:02

Matt Wales


1 Answers

The problem isn't to do with the type of parameters - the PDO documentation on this topic is incorrect. The following posts describe the workaround. http://forums.mysql.com/read.php?98,167022,222800#msg-222800 and OUT or INOUT argument 1 for routine xxx is not a variable or NEW pseudo-variable in BEFORE trigger.

like image 113
Matt Wales Avatar answered Feb 15 '26 20:02

Matt Wales



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!