Can someone help.. i'm not the best with php.. and have been asked by the boss to remove a crapload of emails (listed in a txt file one per line) from a ms access database on the intranet. I have played with mysql and php, and figured something like the following script would work IF it was mysql.. can someone help me with a MS access equivalent?

<?

$db_name = "email_list";  //mysql database
$db_user ="username";   //mysql username
$db_pass = "password";  //mysql password

$unwanted_emails = "emails.txt";  //text file of emails to be removed, one per line

$conn = mysql_connect("localhost",$db_user,$db_pass) or
    die ("Could not connect to localhost");

mysql_select_db($db_name, $conn) or
    die ("Could not select database ($db_name).");

$email_addrs = file ($unwanted_emails);
$file_length = count($email_addrs);

foreach ($email_addrs as $line_num => $line) {
    $query = "delete from emails where email_add = '$line'");
    mysql_query($query) or
        die(mysql_error());
    echo "\"$line\" : <font color=blue>[removed {$line_num} of $file_length]</font>";
}
echo "Total of <b>$file_length</b> emails removed.";

?>

    To connect to an MSAccess database you need an ODBC connection. Set up a DSN called emailAccess, for example, and set it to point to your access mdb file (I've only ever got it work with access when the mdb file is on the same server as the web server)

    Having done that, you need to use the ODBC functions instead of the mysql database functions. Unfortunately you can't just do a global replace of 'mysql' with 'odbc' as the function parameters are sometimes different)

    The difference which affect your functions are

    $conn = mysql_connect("localhost",$db_user,$db_pass)
    becomes
    $conn = odbc_connect("DSNname",$db_user,$db_pass)

    mysql_query($query)
    becomes
    odbc_exec($conn, $query)

    mysql_select_db has no equivalent (you specify $conn in the queries)

    hth

      Thanks champ.. will try it out! 🙂

        4 months later

        first you need to go to odbc32 or whatever and goto system dsn and put the database email_list then i give you da code

        <? 
        
        $db_name = "email_list"; //mysql database okay
        $db_user ="username"; //mysql username good 
        $db_pass = "password"; //mysql password will work
        
        $unwanted_emails = "emails.txt"; //text file of emails to be           removed, one per line 
        
        $conn = odbc_connect($db_name, $db_user, db_pass);
        
        $email_addrs = file ($unwanted_emails); 
        $file_length = count($email_addrs); 
        
        foreach ($email_addrs as $line_num => $line) { 
        $query = "delete from emails where email_add = '$line'"); 
        odbc_do($conn, $query);
        echo "\"$line\" : <font color=blue>[removed {$line_num} of $file_length]</font>"; 
        } 
        echo "Total of <b>$file_length</b> emails removed."; 
        ?>
        
          6 days later

          This is not exactly the same issue as I have, but all you folks seem very knowledgeable and clued-up. So, I hope you can help!

          The hosting conmpany I am with offers Access Databases (which I am using), but I wish to move to a host who only offers PHP and MySQL. Does does one go about converting my Access Database? Or connecting to it?

          Is this is a big or small thing.

          I have about 10 databases, some of which are part of various ASP scripts, such as mailinglists, admanagers, etc.

          Thanks for your help.

            I'd transfer the data from access to mysql. Access isn't exactly the best choice for web sites. Theres a product called MyAccess (addin for access) which makes the task easy. Or save your data in delimited files and load into mysql.

              Write a Reply...