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

Source: Internet
Author: User
The method of using ADODB to allow PHP to operate the ACCESS database has been studied in PHP for the past two days, mainly to cope with the background knowledge needed to learn AJAX, I have always had an inexplicable sense of closeness to open-source things, and don't want to pay for things. this is really amazing. because MYSQL databases in general space have to spend money separately, I plan to use ACCESS for the time being, the results show that the data is too big. PHP has 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:

The 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"
N ";
}


If ADODB is used, the following program returns the same result.

The 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"
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:

The code is as follows:

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"

". $ Result-> fields [$ I]."
";
$ Result-> MoveNext ();

}
?>


ADODB can download http://phplens.com/phpeverywhere/ href = "http://phplens.com/phpeverywhere/" target = _ blank> 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.