Database Code

  Home arrow Database Code arrow MySQL to Access Transfer
DATABASE CODE

MySQL to Access Transfer
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2002-01-18

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    This is the easiest way I know how to get data from a MySQL database to and Microsoft Access database. Its not automatic, but its by far the best way to move tables from MySQL to Access. A single function generates a CSV file (with Access string escaping) that can be easily downloaded and "imported" into Access.

    By : woodys

    <?

    function mysql2access($dbconnection, $tablename,$outfile)
    {
    //this function produces a CSV text file (with Microsoft string escaping) that can be processed by Microsoft Access's data import funcitonality

    //parameters:
    // $dbconnection: this is a the MySQL connection handle returned by mysql_connect()
    // $tablename: CSV files can only handle one table at a time, so we pass the table name here
    // $outfile: output filename (remember that the subdir needs permissions that PHP can write to) (i.e. CHMOD 777)


    //open the the output file
    $fp=fopen($outfile,"w");

    $res=mysql_query("select * from $tablename",$dbconnection);
    $nr=mysql_num_rows($res);

    //enumerate field types (and buffer them in an array for performance reasons)
    $nf=mysql_num_fields($res);
    for ($b=0;$b<$nf;$b++)
    {
    $c=mysql_field_type($res,$b);

    switch($c)
    {

    case "int":
    case "real":
    case "unknown":
    case "timestamp":
    case "numeric":
    case "decimal":
    default:
    $is_numeric[$b]=true; break;

    case "string":
    case "date":
    case "datetime":
    case "time":
    case "blob":
    case "char":
    case "varchar":
    case "text":
    $is_numeric[$b]=false; break;
    }
    }



    for ($a=0;$a<$nr;$a++)
    {
    $row=mysql_fetch_row($res);

    for ($b=0;$b<$nf;$b++)
    {
    $c=mysql_field_type($res,$b);

    if ($is_numeric[$b]==false)
    {
    $s=access_escape_string(strval($row[$b]));
    fputs($fp,"\"$s\"");
    }
    else
    {
    fputs($fp,strval($row[$b]));
    }

    if ($b<($nr-1)) fputs($fp,", ");

    }

    fputs($fp,"\n");
    }

    fclose($fp);

    }

    function access_escape_string($s)
    {
    $s2="";
    $sl=strlen($s);

    for ($a=0;$a<$sl;$a++)
    {
    $c=substr($s,$a,1);

    if ($c=="\"") $c="\"\"";

    $s2.=$c;
    }

    return $s2;
    }

    ?>

    /* example */
    <?

    include("mysql2access.h");

    //mysql2access needs the db connection handle
    $con=mysql_connect("localhost","username","password");
    $db=mysql_select_db("dbname",$con);

    mysql2access($con,"promo","/home/sites/site90/web/backup/promo.txt");
    mysql2access($con,"orders","/home/sites/site90/web/backup/promo.txt");
    mysql2access($con,"users2","/home/sites/site90/web/backup/users2.txt");
    mysql2access($con,"mailinglist","/home/sites/site90/web/backup/mailinglist.txt");

    ?>
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Database Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    DATABASE CODE ARTICLES

    - Converting CSV Files to MySQL Insert Queries...
    - Examples and Tools for Database Design
    - Relationships, Entities and Database Design
    - Modeling and Designing Databases
    - Data extract to Excel
    - Oracle database class 0.76
    - The opposite of mysql_fetch_assoc
    - On line Thermal Transmitance Calculation
    - pjjTextBase
    - PHP Object Generator
    - FastMySQL
    - RC4PHP
    - SQL function with integrated sprintf()
    - DB Interaction Classes v1.1
    - deeMySQLParser

    Developer Shed Affiliates

     



    © 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap