How to Use ADODB to allow PHP to operate the ACCESS database

Source: Internet
Author: User

I have been studying PHP for the past two days, mainly to cope with some background knowledge required for learning AJAX. I always have an inexplicable closeness to open-source things, money-less things are really amazing. because MYSQL Databases in general space have to spend money separately, I plan to use ACCESS for the time being. The result is that I will be the first to check the information, in the past, PHP performed different operations on different databases ..... so I searched some blogs and found many ACCESS methods, but they were not satisfactory. Finally I found the ADODB. it is a PHP class and has a built-in Operation Method for most of the databases you have ever seen. for example, PHP usually operates on a MYSQL instance like this:

Copy codeThe Code is as follows: $ db = mysql_connect ("localhost", "root", "password ");
Mysql_select_db ("mydb", $ db );
$ Result = mysql_query ("SELECT * FROM employees", $ db );
If ($ result = false) die ("failed ");
While ($ fields = mysql_fetch_row ($ result )){
For ($ I = 0, $ max = sizeof ($ fields); $ I <$ max; $ I ++ ){
Print $ fields [$ I]. '';
}
Print "<br> n ";
}
If ADODB is used, the following program returns the same result.

Copy codeThe Code is as follows: include ("adodb. inc. php ");
$ Db = NewADOConnection ('mysql ');
$ Db-> Connect ("localhost", "root", "password", "mydb ");
$ Result = $ db-> Execute ("SELECT * FROM employees ");
If ($ result = false) die ("failed ");
While (! $ Result-> EOF ){
For ($ I = 0, $ max = $ result-> FieldCount (); $ I <$ max; $ I ++)
Print $ result-> fields [$ I]. '';
$ Result-> MoveNext ();
Print "<br> n ";
}
Then, if you want to use another database, change the connection name of the ADOConnection, and ACCESS will use

$ Db = NewADOConnection ('access ');

The complete code is as follows:

 Copy codeThe Code is as follows: <? Php
Include ("adodb/adodb. inc. php ");
$ Db = ADONewConnection ('access ');
$ Dsn = "Driver = {Microsoft Access Driver (*. mdb)}; Dbq = d: \ self \ myphp \ book. mdb; Uid =; Pwd = ;";
$ Db-> Connect ($ dsn );
$ Result = $ db-> Execute ("SELECT * FROM data ");

If ($ result = false) die ("failed ");
While (! $ Result-> EOF ){
For ($ I = 0, $ max = $ result-> FieldCount (); $ I <$ max; $ I ++)
Print "<div style = 'border: 1px solid #000000; '>". $ result-> fields [$ I]. "<br/> ";
$ Result-> MoveNext ();

}
?>
ADODB can download http://phplens.com/phpeverywhere/ here

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.