Showing results 1 to 2 of 2

Thread: [PHP] Export MySQL Contact Information as Outlook Contacts

  1. #1
    Senior Member

    Crusader
    Dyndrilliac's Avatar
    Join Date
    Jun 2005
    Location
    Jacksonville, FL, USA
    Posts
    3,618

    Default [PHP] Export MySQL Contact Information as Outlook Contacts

    Recently I helped some of the guys in the IT department here at UNF figure out a robust way to take a database of people and extract the last name, first name, phone number, and email address of each person and build a contact list that can then be imported into Outlook or Thunderbird (In this situation, an Exchange server is not viable). I searched for something to meet these needs but ended up writing my own (surprisingly simple) code to do the job. This example assumes you have a table of people in a MySQL database with at least the fields 'lname', 'fname', 'phone', and 'email'.

    get_csv_file.php:
    Code:
    <?php    header('Content-Type: text/csv');
        header('Content-Disposition: attachment;filename=Contacts.csv');
        
        require_once "library.php";
        
        $csv_file = fopen("Contacts.csv", "w");
        $db_server = db_login();
        
        $result = mysql_query("SELECT * FROM contact_table ORDER BY lname");
        if (!$result) die("Database access failed: ".mysql_error());
        $rows = mysql_num_rows($result);
        
        // Header for Outlook Field Mapping
        fwrite($csv_file, "\"Last Name\",\"First Name\",\"Primary Phone\",\"E-mail Address\"".PHP_EOL);
        
        for ($j = 0; $j < $rows; $j++)
        {
            $lname = mysql_result($result, $j, 'lname');
            $fname = mysql_result($result, $j, 'fname');
            $phone = mysql_result($result, $j, 'phone');
            $email = mysql_result($result, $j, 'email');
            
            // Individual Contacts
            fwrite($csv_file, "\"".$lname."\",\"".$fname."\",\"".$phone."\",\"".$email."\"".PHP_EOL);
        }
        
        mysql_close($db_server);
        fclose($csv_file);
        readfile("Contacts.csv");
    ?>
    The Ultimate Guide Thread
    Quote Originally Posted by Ethernet Networking Bible
    Thou shalt switch where thy can, and route where thy must.

  2. #2

    Default

    cool, now make it so these people could upload an updated CSV file that in turn updates the MySQL database. simple yet effective front-end for everyone to manage stuff.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Free NNTP / UseNet / Newsgroup add-in for MSO Outlook 2007?
    By Dyndrilliac in forum Hardware and Software
    Replies: 0
    Last Post: 10-30-2009, 11:11 AM
  2. Glasses or Contacts?
    By kill_urself[SA] in forum Flame Board
    Replies: 43
    Last Post: 09-20-2009, 04:29 PM
  3. Colored Contacts
    By Ghost-Flight in forum Flame Board
    Replies: 6
    Last Post: 07-31-2008, 06:20 AM
  4. Contacts exam
    By Pickles the Drummer in forum General Chat
    Replies: 22
    Last Post: 10-26-2005, 05:05 PM
  5. Rant: Warden Outlook
    By Infinite in forum Starcraft/Brood War
    Replies: 19
    Last Post: 08-26-2005, 09:06 PM

Posting Rules

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •