PHP access data connection and read save edit data implementation code

Source: Internet
Author: User
Tags driver manager
A php + access message source code will be provided for your reference. I am familiar with phpaccess operations. The code is as follows:


$ Conn = new com ("ADODB. Connection ");
$ Connstr = "DRIVER = {Microsoft Access Driver (*. mdb)}; DBQ =". realpath ("www.jb51.net/db.mdb ");
// The com interface is required for connecting to access.
$ Conn-> Open ($ connstr );
$ Rs = new com ("ADODB. RecordSet ");
// Query and display data
$ Rs-> Open ("select * from szd_t", $ conn, 1, 1 );
While (! $ Rs-> eof ){
$ F = $ rs-> Fields (1 );
Echo $ f-> value;
$ Rs-> MoveNext ();
}
// The following describes how to save the php access database tutorial.
$ SQL = "insert into szd_t (title) values ('www .jb51.net ')";
$ Rs-> Open ($ SQL );
Echo 'saved successfully ';
// Edit the php access database
$ SQL = "Update szd_t set title = 'jb51. net' where id =". $ _ GET ['id'];
$ Rs-> Open ($ SQL );
Echo 'edited successfully ';
// Delete data
$ SQL = "delete from szd_t where id =". $ _ GET ['id'];



Three methods for connecting php to the Access Database
Recently I want to change an asp website to php, but the space does not support mysql databases, so I have to use the access database, but I used php + mysql in the past, php + access database programming has never been done yet.
Thanks to the party, cctv, and the search engine. I did not find a good article. I 'd like to share it with you.
There are three methods to connect to Access in PHP.
(1) create a system data source and use the ODBC function provided by PHP.
(2) You can also use the ODBC function of PHP without creating a data source.
Open DateBase Conection (ODBC) is one of Windows Open Server APIs (WOSA. A data source is a named connection to the database. An ODBC driver is required for different types of databases to be connected by applications. ODBC APIs are mainly designed for RDBMS users/servers. However, ODBC drivers can also be used to connect desktop database files, worksheets, and flat files. ODBC uses the Odbcinst. dll library to set and clear data sources. Odbcad32.exe is an independent 32-bit executable application used to create an ODBC data source. it has its corresponding icon Control Panel in the Control Panel.
The ODBC driver manager opens the ODBC driver for the data source and sends SQL statements to the driver. After the client/server RDBMS processes a select query, the ODBC driver returns the value to the application. When an insert, update, or delete statement is executed, the driver returns the number of rows affected by the query. Phperz.com
The following describes how to use ODBC to connect to the Access database in PHP. Use $ connstr = "DRIVER = Microsoft Access Driver (*. mdb) to set the data DRIVER. the realpath () function is used to obtain the relative path of the database. This method is used to connect the Access database to the odbc_connect () function of PHP. the function declaration is as follows: www.phperz.com

The code is as follows:


Resourse odbc_connect (string dsn, string user, string password [, int cursor_type])
Dsn: the name of the system dsn.
User: user name of the database server.
Password: password of a user on the database server.
Cursor_type: cursor type.


The code is as follows:

The code is as follows:


$ Connstr = "DRIVER = Microsoft Access Driver (*. mdb );
DBQ = ". realpath (" bookinfo. mdb ");
$ Connid = odbc_connect ($ connstr, "", "", SQL _CUR_USE_ODBC );


(3) use Microsoft's ADODB database driver. ActiveX Data Objects (ADO) is a database access technology for Microsoft open database applications. It is designed to work with the new data Access layer ole db Provider to provide Universal data Access ). Ole db is a low-level data access interface that can be used to access various data sources, including traditional relational databases, email systems, and custom business objects. ADO technology greatly simplifies ole db operations, because ADO encapsulates a large number of COM interfaces used in ole db programs, so ADO is a high-level access technology. Php programmer site
Based on the common Object Model (COM), ADO provides access technology in multiple languages. PHP uses a pre-defined class COM to manipulate the Access database using the ADO method. This class is described in detail as follows: www.phperz.com

The code is as follows:


String com: com (string module_name [, string server_name [, int codepage])
Module_name: name or class-id of the requested component. Www ~ Phperz ~ Com
Server_name: name of the DCOM server.
Codepage: specifies the code page used to convert a PHP string to a UNICODE string, and vice versa. The optional values include CP_ACP, CP_MACCP, CP_OEMCP, CP_SYMBOL, CP_THREAD_ACP, CP_UTF7, and CP_UTF8.

PHP uses the com class and ADO method to access the database:
[Code]
$ Conn = new com ("ADODB. Connection ");
$ Connstr = "DRIVER = {Microsoft Access Driver (*. mdb)}; DBQ =". realpath ("bookinfo. mdb ");
$ Conn-> Open ($ connstr );



This is an article sent by other network friends. In combination, the script home will provide a message source code for php + access. For more information, see. I am familiar with php access operations.
Although PHP is rarely used to link ACCESS, it is quite good to occasionally use it to export data.

The code is as follows:


/*
Create an ADO Connection
*/
$ Conn = @ new COM ("ADODB. Connection") or die ("ADO Connection faild .");
$ Connstr = "DRIVER = {Microsoft Access Driver (*. mdb)}; DBQ =". realpath ("DATUM/cnbt. mdb ");
$ Conn-> Open ($ connstr );
/*
Create record set query
*/
$ Rs = @ new COM ("ADODB. RecordSet ");
$ Rs-> Open ("select * from dbo_dirs", $ conn, 1, 3 );
/*
Read data cyclically
*/
While (! $ Rs-> eof ){
Echo "$ rs-> Fields [" title "]-> Value;
Echo"
";
$ Rs-> Movenext (); // move the record set pointer down
}
$ Rs-> close ();
?>



Function description and example
Although PHP is rarely used to link ACCESS, it is quite good to occasionally use it to export data.

Php access simple message source code

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.