asked on
$sth = $this->db->prepare('CALL `oss`.`sp_customer_insert`(?,?,?,?)');
$sth->bindParam(1, $data->quicken_id, PDO::PARAM_STR, 20);
$sth->bindParam(2, $data->old_id, PDO::PARAM_STR, 25);
$sth->bindParam(3, $data->status, PDO::PARAM_STR, 10);
$sth->bindParam(4, $data->name, PDO::PARAM_STR, 60);
$sth->execute();
So question is where and how do i specify the output field in my prepare statment and how do i retrieve the value of LIDASKER
$dsn = 'mysql:dbname=ee;host=localhost';
That code was sucessfuly run against my MySQL database
but have issue getting the value returned via pdoYou retrieve it as you would a SELECT. To make this easier
SELECT @@IDENTITY AS lastId;
And in the PHP code change $result = $pdo->query("CALL agentname('James Bond')");
foreach($result as $r) {
print_r($r);
}
would change to$result = $pdo->query("CALL addagent('Batman')");
$row = $result->fetch();
$id = is_array($row) ? $row['lastId'] : false;
if ($id) {
echo "Successful insert [{$id}]";
}
else {
echo "Failed";
}
ASKER
REATE DEFINER=`root`@`%` PROCEDURE `sp_test`(
IN p_name varchar(45),
OUT LID Bigint
)
BEGIN
insert into tbl_test (name)
Values (p_name);
SET LID = LAST_INSERT_ID();
END
Sticking with my syntax like$sth = $this->db->prepare('CALL`sp_test`(?)');
$sth->bindParam(1, $data->name, PDO::PARAM_STR, 20);
$sth->execute();
How would i change the call to get my LID ?
ASKER
$row = $sth -> fetch();
echo $row ["lastId"];
In your stored procedure (as per my code)SELECT @@IDENTITY AS lastId;
my question was how to create a output parm in a prepare statment via bindParm and then get the value returned when using PDOI have answered this twice already - but will do so again.
ASKER
$sth->execute();
$row = $sth->fetch();
echo $row["lastId"];
returns "Syntax error"
<?php
$dsn = 'mysql:dbname=db;host=localhost';
$pdo = new PDO($dsn, 'user', 'password');
$statement = $pdo->prepare("CALL addagent('Jason Bourne')");
$result = $statement->execute();
$row = $statement->fetch();
$id = is_array($row) ? $row['lastId'] : false;
if ($id) {
echo "Successful insert [{$id}]";
}
else {
echo "Failed";
}
This returns the followingSuccessful insert [22]
Therefore, the code works - if it is not working for you then it is an implementation issue and you will need to post the code you are using along with a detailed list of errors - including an indication which lines of the code you post the error relates to.
ASKER
CREATE DEFINER=`root`@`%` PROCEDURE `sp_test`(
IN p_name varchar(45)
)
BEGIN
insert into tbl_test (name)
Values (p_name);
SELECT LAST_INSERT_ID() as LastID;
END
and i call it from my sql editor like this in workbenchCALL`sp_test`('Mueller');
which returns me the LastID, problem is how to get this value in PDO after calling a stored procedure via prepare statments
SELECT LAST_INSERT_ID() as LastID;
ToSELECT @@IDENTITY as LastID;
problem is how to get this value in PDO after calling a stored procedure via prepare statmentsI have given you working code to show you how to do this - see the code in my second to last post.
ASKER
CREATE DEFINER=`root`@`%` PROCEDURE `sp_test`(
IN p_name varchar(45)
)
BEGIN
insert into tbl_test (name)
Values (p_name);
SELECT @@IDENTITY as LastID;
END
and thats my code public function insertTest()
{
$statement = $this->db->prepare("CALL `sp_test`('Mueller')");
$result = $statement->execute();
$row = $statement->fetch();
$id = is_array($row) ? $row['lastId'] : false;
if ($id) {
echo "Successful insert [{$id}]";
} else {
echo "Failed";
}
}
$name = "Chris";
$stmt = $dbh->prepare("CALL sp_test(?, ?)");
$stmt->bindParam(1, $name);
$stmt->bindParam(2, $id, PDO::PARAM_INT, 11);
$stmt->execute();
var_dump($id);
It doesn't work, so you need a work around. Julian has shown you one way to do that, but if you still want to use your original SP with the output parameter, then you need a little additional code. Effectively, once you've called the stored procedure, you then need to send another query to the DB to retrieve the Output value. Something like this:$sth = $this->db->prepare('CALL sp_test(?, @id)');
$sth->bindParam(1, $data->name);
$sth->execute();
// now we need the additional code to retrieve the OUTPUT value:
$id = $this->db->query("SELECT @id")->fetchColumn();
SELECT @@IDENTITY as LastID;
IS NOT THE SAME ASSELECT @@IDENTITY as lastId;
Case matters.ASKER
public function insertCustomer($data)
{
$statement = $this->db->prepare("CALL `sp_test`('Mueller')");
$result = $statement->execute();
$row = $statement->fetch();
$id = is_array($row) ? $row['LastId'] : false;
if ($id) {
echo "Successful insert [{$id}]";
} else {
echo "Failed";
}
}
I still get Syntax Error
ASKER
CREATE DEFINER=`root`@`%` PROCEDURE `sp_test`(
IN p_name varchar(45)
)
BEGIN
insert into tbl_test (name)
Values (p_name);
SELECT @@IDENTITY as LastID;
END
CREATE TABLE `tbl_test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=latin1;
ASKER
CREATE
PROCEDURE `ee`.`addagent`(IN agentname VARCHAR(255))
BEGIN
INSERT INTO `agents` (`name`) VALUES (agentname);
SELECT LAST_INSERT_ID() AS lastId;
END
PHP<?php
$dsn = 'mysql:dbname=db;host=localhost';
$pdo = new PDO($dsn, 'user', 'password');
$statement = $pdo->prepare("CALL addagent('Jack Bauer')");
$result = $statement->execute();
$row = $statement->fetch();
$id = is_array($row) ? $row['lastId'] : false;
if ($id) {
echo "Successful insert [{$id}]";
}
else {
echo "Failed";
}
The Author states he received a syntax error with code from this post although he does not give us any indication what line the error occurred on nor what the content of the error was we can possibly guess that it has something to do with the fact that his function declaration included the word 'public' - which is only valid if this is in a class.ASKER
PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.
TRUSTED BY
Open in new window
Example stored procedureOpen in new window
PHP CodeOpen in new window
Note the following in the Stored Procedure
Open in new window
Alternatively you can doOpen in new window