Is it possible to display trigger error message to Webpage?

Viewed 550

I have trigger under certain condition, If condition: false Error occurs. I should display that error message which occurs in database trigger to HTML web page to notify user!

CREATE TRIGGER check_trigger BEFORE INSERT ON your_table
FOR EACH ROW
BEGIN        
    IF (NEW.name = NEW.firstname) THEN
        SIGNAL SQLSTATE '45000' 
            SET MESSAGE_TEXT = 'same names error. Insertion canceled';
    END IF;
END

How to display SET MESSAGE_TEXT="" to web page? If it's not possible any other ways

2 Answers

You can take a look at the documentation here.

It says:

The error values that are accessible after SIGNAL executes are the SQLSTATE value raised by the SIGNAL statement and the MESSAGE_TEXT and MYSQL_ERRNO items. These values are available from the C API:

mysql_sqlstate() returns the SQLSTATE value.

mysql_errno() returns the MYSQL_ERRNO value.

mysql_error() returns the MESSAGE_TEXT value.

Since I'm guessing you're using PHP you can try

if (!$mysqli->query("INSERT INTO your_table SET name='a', firstname='a'")) {
    printf("Error message: %s\n", $mysqli->error);
}

You can look at some like Pusher which is an external rest service that can push messages to your web site.

Related