What are the best practices for storing and comparing email sending limits in a MySQL database?

When storing and comparing email sending limits in a MySQL database, it is important to ensure that the data is stored accurately and securely. One way to achieve this is by using a dedicated table in the database to store the limits and regularly updating them as needed. When comparing the limits, it is best to retrieve the values from the database and perform the comparison in the application logic.

// Connect to the MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "email_limits";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Retrieve email sending limit from the database
$sql = "SELECT limit_value FROM email_limits_table WHERE limit_type = 'daily'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $daily_limit = $row["limit_value"];
    }
} else {
    echo "Error: Email sending limit not found";
}

// Compare email sending limit
if ($emails_sent_today <= $daily_limit) {
    echo "Email sending limit not exceeded";
} else {
    echo "Email sending limit exceeded";
}

// Close database connection
$conn->close();