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);
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.