我写了一个存储过程(mysql)
CREATE PROCEDURE `set_SiteAttendance`(IN _UserName VARCHAR(20) CHARSET utf8, IN _EventDesc VARCHAR(250) CHARSET utf8, IN _EventTime BIGINT(20))
BEGIN
DECLARE _EventID INT;
DECLARE cursorGetID CURSOR FOR
SELECT id
FROM client_pages
WHERE name = _EventDesc
LIMIT 0, 1;
-- try to add a new record to the database
INSERT INTO client_pages (id, name) SELECT (IFNULL(MAX(id), 0) + 1), _EventDesc FROM client_pages;
-- get id from the database records
OPEN cursorGetID;
FETCH cursorGetID INTO _EventID;
-- set the data on the visit of the page in the database
INSERT INTO login_history VALUES (NULL, _UserName, _EventID, _EventTime);
END
当我使用 MySQL Workbench 调用它时,它工作正常。
CALL set_SiteAttendance('MyName', 'page#1', 100);
当我使用 php 调用它时,存储过程在 INSERT 语句上中止:
$query = "CALL set_SiteAttendance('$user_name', '$user_page', $user_ticks)";
mysql_query($query);
mysql_error() 调用问题
Duplicate entry 'page#1' for key 'name'
为什么会发生这种情况以及如何解决?
最佳答案
嗯,这很明显。您在 name
上有唯一索引,因此您的查询INSERT INTO client_pages (id, name) SELECT (IFNULL(MAX(id), 0) + 1), _EventDesc FROM client_pages;
只能使用相同的 _EventDesc
参数运行一次。
删除索引或更改过程的逻辑。
关于php - 中断php中存储过程(mysql)的执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39612825/