简体   繁体   中英

interrupt the execution of the stored procedure (mysql) in php

I wrote a stored procedure (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

When I call it using MySQL Workbench, it works correctly.

CALL set_SiteAttendance('MyName', 'page#1', 100);

When I call it using php, then the stored procedure aborts on the INSERT statement :

$query = "CALL set_SiteAttendance('$user_name', '$user_page', $user_ticks)";
mysql_query($query);

mysql_error() call issues

Duplicate entry 'page#1' for key 'name'

why it happens and how to fix?

Well, that's pretty obvious. You have a unique index on name , so your query INSERT INTO client_pages (id, name) SELECT (IFNULL(MAX(id), 0) + 1), _EventDesc FROM client_pages; can only be run once with same _EventDesc parameter.

Remove the index or change the logic of your procedure.

I decided my problem:

INSERT IGNORE INTO client_pages (id, name) SELECT (IFNULL(MAX(id), 0) + 1), _EventDesc FROM client_pages;

Error (name was found) was ignored and procedure is not interrupted

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM