php – How do I know if a record has been added to the database?

Question:

Hello, I'm working on an alert system, when more than normal data is registered in the database an alert has to appear on the page the user is browsing. The problem is that I don't know how to check if an element has been added to the database.

I'm getting my data automatically from a PCD (weather station), if any of this data exceeds the limit registered by ADM, an alert will be shown to the user.

I have the following tables in my database:

CREATE TABLE PARAMETROS_DE_ALERTAS (
    PRA_ID INTEGER AUTO_INCREMENT PRIMARY KEY,
    PRA_VALOR_MAXIMO INTEGER(5) NOT NULL,
    PRA_VALOR_MINIMO INTEGER NOT NULL,
    PRA_COR_MINIMA VARCHAR(30) NOT NULL,
    PRA_COR_MAXIMA VARCHAR(30) NOT NULL,
    SEN_ID INTEGER NOT NULL
);

This table is the parameter for the alerts, where the minimum and maximum of the measurement coming from the PCD will be registered for it to be an alert.

CREATE TABLE MEDICOES (
    MED_ID INTEGER AUTO_INCREMENT PRIMARY KEY,
    MED_DADO VARCHAR(10) NOT NULL,
    MED_DATA_HORA_MEDICAO DATETIME NOT NULL,
    API_ID INTEGER,
    SEN_ID INTEGER NOT NULL
);

This is the measurement table, where you will store the data coming from the PCD.

CREATE TABLE ALERTAS_CRITICOS (
    ALC_DATA_HORA_ALERTA DATETIME NOT NULL,
    ALC_ID INTEGER AUTO_INCREMENT PRIMARY KEY,
    ALC_VALOR_MEDICAO FLOAT NOT NULL,
    SEN_ID INTEGER NOT NULL
);

The system must add a record to this table if the measurement data is greater or less than the parameters. After this critical alert is registered, the system should show an alert to the user who is browsing any page on my site.

Problem is, I don't know how to check if a record has been added to the database.

Answer:

You can use a bank function to give this insert. Inside this function you do the necessary INSERT and can give a SELECT right after returning true or false so the application knows if it was successful.

Scroll to Top