vaxzz
vaxzz

Reputation: 98

PHP - Can i count the amount of rows in my db and execute something when it exceeds 10 rows?

I've built a web app that uses SNMP to log all errrors that happen on my network device. Thats done. However, i want to know if there is a way in PHP to count the records in the database and let it for example mail me if there are more then 10 errors for that specific item.

Lets say i have network device A, and i log a certain activity such as harddisk space. What i want is that if that certain activity reaches more then 10 rows in my database, it automatically sends me a mail.

The mail part shouldn't be a problem, but i'm not sure how to count those rows in my sql database especially because i log way more activitys and network devices. I want this done for all the activity's i log.

My log table has these columns:

What is the best way to approach this? All help would be greatly appreciated.

Upvotes: 1

Views: 46

Answers (2)

Jeffrey Hitosis
Jeffrey Hitosis

Reputation: 325

Set your task in the crontab that will monitor your table for every hour (or your desired schedule).

Example:

index.php

    $result=mysqli_query($con, "SELECT * from yourtable GROUP BY network_device HAVING count(*) > 10");

   while ($row = mysql_fetch_assoc($result)) {
       //send email to you.
   }

In cron tab (for linux):

Open terminal and type crontab -e Set your schedule every hour. This will scan your table every hour and send execute your code.

0 * * * * php -f /file/to/index.php

Upvotes: 0

Shakti Phartiyal
Shakti Phartiyal

Reputation: 6254

You can get the rows in the DB like this:

$sql="SELECT id FROM myTable";
if ($result=mysqli_query($con,$sql))
{
    $rowcount=mysqli_num_rows($result);
    if($rowcount > 10)
    {
            //Do your mailing
    }
}

The other more efficient approach would be:

$result=mysqli_query($con, "SELECT count(*) as total from myTable");
$data=mysql_fetch_assoc($result);
$total = $data['total'];
if($total > 10){
    //Do your mailing
}

Upvotes: 1

Related Questions