Import an Access database into MySQL using PHP

Source: Internet
Author: User
Tags save file

<?phpheader ("content-type:text/html; Charset=utf-8 ");//////convert the Access database to a MySQL SQL statement////Run/////on the command line to connect to the Access database $db = Odbc_connect (" Driver={microsoft Access Driver (*.mdb)}; Dbq= ". Realpath ("./test.mdb"), "", "" ");//Generate SQL file $file =" Test.sql "; Save_file ($file," SET NAMES utf8;\n\n "); $tables = get_ Tables ($db); for ($i =0; $i <count ($tables); $i + +) {$table = $tables [$i];echo ("$i \t$table\n"); $res = Odbc_exec ($db, $ sql = "SELECT * from ' $table '"), $fields = Get_table_fields ($res), $structure _sql = Get_table_structure ($table, $fields). " \ n "; $data _sql = Iconv (" GBK "," UTF-8 ", Get_table_data ($res, $table)." \n\n\n ") Save_file ($file, $structure _sql) save_file ($file, $data _sql);} Echo ("OK");/////////////////////////////////////////////////////////////////////////save File function Save_file ($ File, $data) {$fp = fopen ($file, ' A + '); fwrite ($fp, $data); fclose ($FP);} Gets the data table function Get_tables ($db) {$res = Odbc_tables ($db), $tables = Array (), while (Odbc_fetch_row ($res)) {if (odbc_ Result ($res, "table_type") = = "TABLE") $Tables[] = Odbc_result ($res, "table_name");} return $tables;} Gets the table field function Get_table_fields ($res) {$fields = array (); $num _fields = Odbc_num_fields ($res); for ($i =1; $i <= $num _ Fields $i + +) {$item = array (); $item [' name '] = Odbc_field_name ($res, $i); $item [' len '] = Odbc_field_len ($res, $i); $item [' type '] = Odbc_field_type ($res, $i); $fields [] = $item;} return $fields;} Generate Build Table SqlFunction get_table_structure ($table, $fields) {$primary _key = '; $sql = Array (); foreach ($fields as $item) {if ($ item[' type ']== ' COUNTER ') {$sql [] = "\ t '". $item [' name ']. " ' Int (". $item [' Len '].") Not NULL auto_increment "; $primary _key = $item [' name '];} else if ($item [' type ']== ' VARCHAR ') {$sql [] = "\ t '". $item [' name ']. " ' varchar (". $item [' Len '].") Not NULL DEFAULT ' ";} else if ($item [' type ']== ' LongChar ') {$sql [] = "\ t '". $item [' name ']. " ' Text not NULL ';} else if ($item [' type ']== ' INTEGER ') {$sql [] = "\ t '". $item [' name ']. " ' Int (". $item [' Len '].") Not NULL DEFAULT ' 0 ' ";} else if ($item [' type ']== ' SMALLINT ') {$sql [] = "\ t '". $item [' name ']. " ' SmallInt (". $item [' Len '].") Not NULL DEFAULT ' 0 ' ";} else if ($item [' type ']== ' REAL ') {$sql [] = "\ t '". $item [' name ']. " ' tinyint (1) Not NULL DEFAULT ' 0 ' ";} else if ($item [' type ']== ' DATETIME ') {$sql [] = "\ t '". $item [' name ']. " ' DateTime not NULL ';} else if ($item [' type ']== ' CURRENCY ') {$sql [] = "\ t '". $item [' name ']. " ' Float not NULL DEFAULT ' 0 ';} else{$sql [] = "\ t '". $item [' name ']. " ' varchar (255) Not NULL DEFAULT ' ";}} Return "CREATE TABLE IF not EXISTS ' $table ' (\ n". Implode (", \ n", $sql). ( $primary _key? ", \n\tprimary Key ('". $primary _key. " ') \ n ":" \ n ").") Engine=myisam DEFAULT Charset=utf8; ";} Get Table Data function Get_table_data ($res, $table) {$row _sql = array (), while ($row = Odbc_fetch_array ($res)) {$row _sql[] = get_ Row_sql ($table, $row);} return implode ("\ n", $row _sql);} Generate Insert SqlFunction get_row_sql ($table, $row) {$keys = Array_keys ($row); $values = Array_values ($row); for ($i =0; $i < Count ($values), $i + +) {$values [$i] = Addslashes ($values [$i]);} Return "INSERT into". $table. " (' ". Implode (" ', ' ", $keys)." VALUES (' ". Implode (" ', ' "), $values). "');";}? >

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.