Php converts an access database to a mysql database

Source: Internet
Author: User
This article describes how php converts an access database to a mysql database. it can read and write access database data to a mysql database, for more information about how php converts an access database to a mysql database, see the following example. Share it with you for your reference. The specific analysis is as follows:

The data collected by myself is ACCESS, but my program is mysql, so I wrote a program, and the program went three steps. I would like to share with my friends who like to operate the database.

1. connect to mysql with the following code:

The code is as follows:

$ Dbhost = "localhost: 3307"; // server
$ Db = "dataname"; // Database
$ Dbuser = "root"; // User name
$ Dbpass = "********"; // password
$ Db_qianzhui = "sdb _"; // table prefix
$ Link = mysql_connect ($ dbhost, $ dbuser, $ dbpass );
If (! $ Link) echo "link failed! ";
Else echo "successful !!! ";
Mysql_query ("set names 'utf8 '");
Mysql_query ("set character set UTF8"); // >>> solve chaos
Mysql_query ("SET CHARACTER_SET_RESULTS = utf8'"); //> code
Mysql_select_db ("{$ db }");


2. connect to the access Database. the code is as follows:

The code is as follows:

$ Datapath = "data. mdb ";
$ Fenlei = "101 ";
$ Conn = new com ("ADODB. Connection ");
$ Connstr = "DRIVER = {Microsoft Access Driver (*. mdb)}; DBQ =". realpath ($ datapath );
$ Conn-> Open ($ connstr );
$ Rs = new com ("ADODB. RecordSet ");
$ Rs-> Open ("select * from information where lanmu3id = 13", $ conn,); // query


3. read and write operations as follows:

The code is as follows:

While (! $ Rs-> eof ){
// $ F = $ rs-> Fields (1); // 1. assign a value (* for debugging)
// Echo $ f-> value; // 2. check whether it can be written or read (* for debugging)
// Assign values to the variables respectively. 'gb2312' and 'utf-8' are used to solve the garbled problem!
$ Id = iconv ('gb2312', 'utf-8', $ rs-> Fields (ID)-> value); // ID is an access field, or a number
$ Name = iconv ('gb2312', 'utf-8', $ rs-> Fields (title)-> value );
$ Jianjie = iconv ('gb2312', 'utf-8', $ rs-> Fields (1)-> value );
$ Content = iconv ('gb2312', 'utf-8', $ rs-> Fields (content)-> value );
$ Down = iconv ('gb2312', 'utf-8', $ rs-> Fields (download)-> value );

Echo "". $ name. "already inserted
"; // Output insert record
/// Execute insert in sequence and the value is a variable
$ SQL = "INSERT INTO 'shopex '. 'SDB _ article' ('article _ id', 'node _ id', 'title', 'content', 'uptime', 'ifpub', 'align ', 'filetype', 'filename', 'orderlist', 'disabled ') VALUES ('',' {$ fenlei} ',' {$ name }', '{$ info}', '123', '1', Null, Null, NULL, NULL, 'false ')";
//// The above is the SQL statement inserted into the MYSQL database //////
Mysql_query ($ SQL); // execute the statement !!!
/// Echo $ rs-> Fields (name)-> value ."
";
$ Rs-> MoveNext ();
//// The user is used to convert the database from ACCESS to mysql,
}

I hope this article will help you with php programming.

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.