我已经看过并阅读了一些关于此的问题,但我无法弄清楚发生了什么。我有一个插入到表中的SP然后只返回一个带有最后一个插入id的列(因为我正在执行多个语句lastInsertID()不起作用。
CREATE DEFINER=`graffixnyc`@`%` PROCEDURE `createUser`(
IN userName VARCHAR(50),
IN firstName VARCHAR(25),
IN lastName VARCHAR(25),
IN pass text)
BEGIN
SELECT FLOOR(RAND() * 0xFFFFFFFF) into @salt;
INSERT INTO `users` (`username`, `first_name`,`last_name`,`salt`,`password`)
VALUES (userName, firstName,lastName, (select
@salt),sha2(concat(md5(pass(select @salt)),256));
SELECT LAST_INSERT_ID() as lastinsert;
END
现在当我执行它时,MySql将返回最后插入记录的值。当我尝试使用PHP访问它时,我得到Null。
$paramUsername = $req->params('username'); // Getting parameter with names
$paramFirstName = $req->params('firstname');
$paramLastName = $req->params('lastname');
$paramPassword = $req->params('password');
$sql = "CALL createUser(:username, :firstname,:lastname,:password)";
try {
$dbCon = getConnection();
$stmt = $dbCon->prepare($sql);
$stmt->bindParam("username", $paramUsername);
$stmt->bindParam("firstname", $paramFirstName);
$stmt->bindParam("lastname", $paramLastName);
$stmt->bindParam("password", $paramPassword);
$stmt->execute();
$row= $stmt->fetch();
$last_id=$row["lastinsert"];
$user->id =$last_id;
我也尝试使用输出Paramater,如下所示:
CREATE DEFINER=`graffixnyc`@`%` PROCEDURE `createUser`(
IN userName VARCHAR(50),
IN firstName VARCHAR(25),
IN lastName VARCHAR(25),
IN pass text,
OUT lastinsert INT)
BEGIN
SELECT FLOOR(RAND() * 0xFFFFFFFF) into @salt;
INSERT INTO `users` (`username`, `first_name`,`last_name`,`salt`,`password`)
VALUES (userName, firstName,lastName, (select
@salt),sha2(concat(md5(pass(select @salt)),256));
SET lastinsert=(SELECT LAST_INSERT_ID());
SELECT lastinsert;
END
$paramUsername = $req->params('username');
$paramFirstName = $req->params('firstname');
$paramLastName = $req->params('lastname');
$paramPassword = $req->params('password');
$sql = "CALL createUser(:username, :firstname,:lastname,:password,
@lastinsert)";
try {
$dbCon = getConnection();
$stmt = $dbCon->prepare($sql);
$stmt->bindParam("username", $paramUsername);
$stmt->bindParam("firstname", $paramFirstName);
$stmt->bindParam("lastname", $paramLastName);
$stmt->bindParam("password", $paramPassword);
$stmt->execute();
$row = $dbCon->query("select @lastinsert;")>fetch();
$last_id=$row["@lastinsert"];
$user->id =$last_id;
当我这样尝试时,我收到了这个错误:
{"error":{"text":SQLSTATE[HY000]: General error: 2014 Cannot execute queries
while other unbuffered queries are active. Consider using
PDOStatement::fetchAll(). Alternatively, if your code is only ever going to
run against mysql, you may enable query buffering by setting the
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.}}
所以我尝试了FetchAll并得到了同样的错误..然后我尝试了这个,我在这里发现它有效,但因为它只返回1行1场,看起来我真的不需要循环通过任何东西:< / p>
$stmt->execute();
$stmt = $dbCon->prepare("select @lastinsert;");
$outputArray = $dbCon->query("select @lastinsert;")->fetchAll();
foreach($outputArray as $row)
{
$last_id=$row["@lastinsert"];
}
那么,如果有更好的方法来做到这一点?我确定有。我是PHP和mySQL的新手,但对SQLServer有很多经验,所以任何见解都会受到赞赏。
答案 0 :(得分:0)
我想通了..这很好用,我不需要输入参数或循环:
SP:
CREATE DEFINER=`graffixnyc`@`%` PROCEDURE `createUser`(
IN userName VARCHAR(50),
IN firstName VARCHAR(25),
IN lastName VARCHAR(25),
IN pass text)
BEGIN
SELECT FLOOR(RAND() * 0xFFFFFFFF) into @salt;
INSERT INTO `users` (`username`, `first_name`,
`last_name`,`salt`,`password`)
VALUES (userName, firstName,lastName, (select @salt),sha2(concat(md5(pass
),(select @salt)),256));
SELECT LAST_INSERT_ID()as lastinsert;
END
PHP:
$paramUsername = $req->params('username');
$paramFirstName = $req->params('firstname');
$paramLastName = $req->params('lastname');
$paramPassword = $req->params('password');
$sql = "CALL createUser(:username, :firstname,:lastname,:password)";
try {
$dbCon = getConnection();
$stmt = $dbCon->prepare($sql);
$stmt->bindParam("username", $paramUsername);
$stmt->bindParam("firstname", $paramFirstName);
$stmt->bindParam("lastname", $paramLastName);
$stmt->bindParam("password", $paramPassword);
$stmt->execute();
$user->id =$stmt->fetchColumn(0);
$dbCon = null;