Asterisk - Getting Results from CDR Database!
So in my previous article, I said I would provide some sample scripts to view information from our CDR database. My script is written in PHP, and we’re just going to do a quick query of the database for answered calls.
To make things neat, I like to write the mysql connection string and variables in seperate files. So my config.php will maintain the database connection information:
<?php
// This is an example of config.php
$dbhost = 'localhost';
$dbuser = 'asterisk';
$dbpass = 'yourpassword';
$dbname = 'asterisk';
?>
Now the mysql connection strings, first to open the connection, the second to close the connection:
<?php
// This is an example opendb.php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die
('Error connecting to mysql');
mysql_select_db($dbname);
?>
<?php
// an example of closedb.php
// it does nothing but closing
// a mysql database connection
mysql_close($conn);
?>
The above files will be included or used in any script we use to query the database. Now let’s get to the queries…
To gather the information of what I’m querying for, I put together a simple HTML form. You can view the form here. And download it’s code here. The form will POST to our process.php which will be discussed in a moment. You will notice several fields on the html form. The only fields that are required are the start date/time and the end date/time.
The PHP that queries the database and outputs the results can be found here.
And the results are something like this:
That’s it! You can download the files mentioned in this article here.

