Count Errors in Database [RESOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
spec36
Forum Commoner
Posts: 28
Joined: Thu Nov 19, 2009 6:07 pm

Count Errors in Database [RESOLVED]

Post by spec36 »

I have a database of some 1 million records, there is one field in the database that contains an error code. I need to search through all records and count the amount of individual errors.

EXAMPLE:
----------------
Field name -> event_desc
ErrorManager.storeError(): name:7725878458375 time: Tue Jun 03 12:59:19 EDT 2010 code: 9909 data:667,D,555,345345354

I need to somehow read the "code: 9909" and count the total amount of them that exist between a specified date range in the database. There are also multiple other error numbers that need to be counted as well.

If someone could steer me in the right direction that would be excellent. What functions should I be using, etc...?

Any help would be greatly appreciated.

Thanks,
Last edited by spec36 on Tue Jul 06, 2010 1:59 pm, edited 1 time in total.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Count Errors in Database

Post by Jonah Bron »

Well, knowing you have that many rows in the table, you'll want to do more with SQL and less with PHP. Do you have a known set of error numbers? If so, you could do something like *this...

Code: Select all

$first_error = 1;
$last_error = 999;
for ($i = $first_error; $i <= $last_error; $i++) {
    $t = mysql_fetch_array(mysql_query(
        'SELECT COUNT(*)
        AS numoferrors
        FROM mytable
        WHERE error = '. $i
    ));
    echo $t['numoferrors'] . ' errors of code '. $i;
}
The only reason I'm not using mysql_real_escape_string() to purify SQL input here is because it's not coming from the user, and we know that it's okay for sure.
* untested
spec36
Forum Commoner
Posts: 28
Joined: Thu Nov 19, 2009 6:07 pm

Re: Count Errors in Database

Post by spec36 »

Thanks for the help. I ended up using the following for this

Code: Select all

select '4444' as 'error code',  count(*) as 'total' from table where field like '%error: 4444%'
Post Reply