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.