我的问题是: 我创建了一个从php到mysql的存储过程。在有人对这种做法说些什么之前,这个程序运作正常。 问题是,当我从php调用该过程并且我知道已经输入了一条记录时,mysql_insert_id(),返回0 ......任何想法为什么?
哦,是的,我的id字段是AUTO_INCREMENT和PRIMARY KEY
P.S 如果有一种简单的方法来格式化我的代码,请告诉我。我把pre放在html中,但它看起来效果不好。
提前Ty。$PROCEDURE_INSERT_UPDATE_BOARD = " CREATE PROCEDURE InsertUpdateBoard(IN `pNewBoardUUID` CHAR(36), IN `pOldBoardUUID` CHAR(36), IN `pSimName` VARCHAR(40), IN `pOwnerName` VARCHAR(64), IN `pOwnerUUID` CHAR(36), IN `pLandmark` VARCHAR(80), IN `pVersion` VARCHAR(10), IN `pManagerUUID` CHAR(36), IN `pBoardURL` CHAR(80), IN `pPassword` CHAR(8)) BEGIN CALL InsertUpdateSims(`pSimName`); CALL InsertUpdateAvatars(`pOwnerName`, `pOwnerUUID`); INSERT INTO boards(`boardUUID`, `simId`, `ownerId`, `landmark`, `version`, `managerId`, `boardURL`, `password`) VALUES(`pOldBoardUUID`, (SELECT rS.id FROM 2starsglobal.sims AS rS WHERE rS.name=`pSimName`), (SELECT rA.id FROM 2starsglobal.avatars AS rA WHERE rA.UUID = `pOwnerUUID`), `pLandmark`, `pVersion`, (SELECT rA2.id FROM 2starsglobal.avatars AS rA2 WHERE rA2.UUID = `pManagerUUID`), `pBoardURL`, `pPassword`) ON DUPLICATE KEY UPDATE `boardUUID`=`pNewBoardUUID`, `landmark`=`pLandmark`, `version`=`pVersion`, `boardURL`=`pBoardURL`; END ";
和Php代码
function InsertUpdateBoard($boardNewUUID, $boardOldUUID, $simName,
$ownerName, $ownerUUID, $boardLandmark,
$versionNumber, $managerUUID, $boardURL)
{
$password = generatePassword(8);
$query = "CALL InsertUpdateBoard('$boardNewUUID', '$boardOldUUID', '$simName',
'$ownerName', '$ownerUUID', '$boardLandmark',
'$versionNumber', '$managerUUID', '$boardURL',
'$password')";
mysql_query($query) or die("ERROR:QUERY_FAILED " . mysql_error());
if(mysql_affected_rows() > 0)
{
if(mysql_insert_id() > 0)
{
echo "SUCCESS,$password";
}
else
{
echo "SUCCESS";
}
}
else
{
echo 'FAILED:BOARD_REGISTRATION_UPDATE';
}
}
答案 0 :(得分:2)
谷歌发现this page
十秒钟请注意,如果您调用MySQL存储过程以插入新记录 然后引用$ db-> insert_id;你会得到0,而不是最后 插入ID。
因此有必要在MySQL存储过程中添加一行 比如
选择last_insert_id()作为intRecordKey;
插入后,以便查询返回新的键值。