How to: MySQL Query to CSV Email

Keeping tabs on your websites and Web servers can be a daunting task, especially when considering the many variables at play in any networking environment. One of the ways you can monitor MySQL databases is by using a PHP script to email you a CSV, or comma separated value, document of the MySQL database so that you can check for changes and errors in your email and save yourself the trouble of having to log into an administrative interface.

Instructions

    • 1

      Open a plain text editing application or your preferred application used to edit PHP code and create a new PHP document.

    • 2

      Initiate the PHP document by inserting the following line of code at the very top of the document:

      <?php

    • 3

      Define what the PHP script will be doing by adding this second line of code:

      require('class.email-query-results-as-csv-file.php');

    • 4

      Initiate the emailCSV function by adding the following two lines of code to your document, respectively replacing localhost, database, username, nd password in the example with the hostname, database name and username and password that you use to log into MySQL:

      $emailCSV = new

      EmailQueryResultAsCsv('localhost','database','username','password');

    • 5

      Specify the table which will be pulled from the emailCSV function by adding the following line of code and replacing "tablename" with the name of the database table from which you are pulling:

      $emailCSV->setQuery("SELECT * FROM tablename");

    • 6

      Add the following lines of code to complete the sending script and specify the email addresses to which the report will be sent, replacing the email addresses in the example with the address which you wish to actually use:

      $emailCSV->sendEmail("sending@emailaddress.com", "receiving@emailaddress.com", "MySQL Query Results as CSV Attachment");

      ?>

Related Searches:

References

Comments

Related Ads

Featured