Michael Self-Made
Michael Self-Made

Reputation: 329

MYSQL EVENT or Something else ? trying to track "clicks for links"

I've been all over this site and google, trying to wrap my head about a solution.

I've created a script that tracks Website traffic that we sell to our members, everything works great and show all the data we want. It will even track the conversions, correctly.

My current problem is - turning off the traffic to the record (member) whose traffic has been all delivered.

After reading some articles, it would seem prefect to run an event, but I can't seem to figure this part out.

It also needs to run numerous times ...

Basically, I am not familiar with Events / Triggers and don't know which to do here

Example; Member has purchase 1000 clicks - all have been delivered, not change status to completed.

CREATE EVENT newEvent
ON SCHEDULE EVERY 1 Minute
DO
UPDATE links SET status = 'completed' WHERE bought = '0';

Database Structure:

ID - Incremental

userid - members userid

bought - shows the remaining clicks

count - shows clicks delivered

pkgamount - stores the clicks bought

Thanks.

Upvotes: 0

Views: 58

Answers (2)

O. Jones
O. Jones

Reputation: 108651

Here's what you need to do to create and run an event. There's some non-intuitive monkey business with delimiters, and with turning on the event scheduler.

DELIMITER $$

SET GLOBAL event_scheduler = ON$$     -- required for event to execute but not create    

CREATE EVENT `Event1`
ON SCHEDULE EVERY 5 MINUTE
ON COMPLETION PRESERVE
COMMENT 'disable delivery when quota used up'
DO
    BEGIN
        UPDATE links SET status = 'completed' WHERE bought = '0';
    END
$$
DELIMITER ;

Please note! You may want to say WHERE bought <= 0 so that you can correctly use an index on your bought column and so you set the status correctly if a particular record falls below zero.

If I were you I'd use this statement.

        UPDATE links SET status = 'completed', bought = 0 WHERE bought <= 0;

Upvotes: 1

Barmar
Barmar

Reputation: 780861

Don't use the event scheduler, use a trigger that runs when updating the links table:

DELIMITER $$
CREATE TRIGGER link_completed
BEFORE UPDATE ON links
FOR EACH ROW
BEGIN
    IF NEW.bought = 0
    THEN SET NEW.status = 'completed';
    END IF;
END; $$
DELIMITER ;

Upvotes: 2

Related Questions