PHP three ways to connect to an Access database _php tutorial

Source: Internet
Author: User
Tags driver manager ole
Not a class, but let's put it here.

Recently want to put an ASP Web site to PHP, helpless space does not support MySQL database, had to use Access database, but used to be used php+mysql,php+access database programming really did not do.
Thank the party, thanks to CCTV, thanks to the search engine, this does not find a good article, special turn around and share with you.//2cto.com
There are 3 ways to connect to access in PHP. PHP Programmer's Home

(1) Create a system data source, using the ODBC function provided by PHP.

PHP Programmer's Home


(2) You can also use PHP's ODBC functions, but do not create a data source.
Open database Connection (open Datebase Conection,odbc) is one of the windows open Server (open service) API (WOSA) products. A data source is a named connection to the database. An ODBC driver is required for the different types of databases that the application wants to connect to. The ODBC API is designed primarily for client/server RDBMS use, but ODBC drivers can also be used to connect desktop database files, worksheets, and flat files. ODBC uses the Odbcinst.dll library to set up and purge data sources. Odbcad32.exe is a stand-alone 32-bit executable application for establishing an ODBC data source, with its corresponding icon control Panel in the dashboard.
PHP Programmer Station//2cto.com


The ODBC Driver Manager opens the ODBC driver for the data source and sends the SQL statement to the driver. After the client/server RDBMS finishes processing 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. 2cto.com

Here's how PHP connects to an Access database using ODBC. Use $connstr= "driver= Microsoft Access DRIVER (*.mdb) to set up data-driven, function Realpath () to get the relative path of the database. Using this method to connect to an Access database is primarily applied to the PHP odbc_connect () function, which is declared as follows://2cto.com

The following is the referenced content:
Resourse odbc_connect (String dsn, string user, string password [, int cursor_type])
DSN: System DSN Name

User: The database server has a username.
PHP Programmer Station


Password: A user password for the database server. PHP Programmer Station

Cursor_type: Cursor type.

PHP Programmer's Home

The code is as follows:


The following is the referenced content:
$connstr = "Driver=microsoft Access DRIVER (*.mdb);
Dbq= ". Realpath (" Bookinfo.mdb ");

$connid =odbc_connect ($connstr, "", "", SQL_CUR_USE_ODBC);


PHP Programmer's Home

(3) using Microsoft's ADODB database driver. ActiveX data Objects (ADO) is a database access technology for Microsoft Open database applications. It is designed to work together with the new data Access layer OLE DB provider to provide universal data access (Universal Date access). OLE DB is a low-level data access interface that provides access to a variety of data sources, including traditional relational databases, e-mail systems, and custom business objects. ADO technology greatly simplifies the operation of OLE DB because ADO encapsulates a large number of COM interfaces used in OLE DB programs, so ADO is a high-level access technology. PHP Programmer Station//2cto.com

ADO technology is based on the Common object Model (COM), which provides access techniques for multiple languages. PHP uses the ADO method to manipulate an Access database by pre-defining class COM. The class is described in detail as follows:

The following is the referenced content:
String com::com (String module_name [, String server_name [, int codepage]])
Module_name: The name or Class-id of the requested component.

The name of the server_name:dcom server.
Codepage: Specifies the code page used to convert the PHP string to a Unicode string, and vice versa. The values for this parameter are CP_ACP, CP_MACCP, CP_OEMCP, Cp_symbol, CP_THREAD_ACP, Cp_utf7, and Cp_utf8.
The code for PHP to use the COM class and access the database using the ADO method is as follows:

The following is the referenced content:
$conn = new COM ("ADODB. Connection ");
$connstr = "Driver={microsoft Access DRIVER (*.mdb)}; Dbq= ". Realpath ("Bookinfo.mdb");

$conn->open ($CONNSTR);

http://www.bkjia.com/PHPjc/486459.html www.bkjia.com true http://www.bkjia.com/PHPjc/486459.html techarticle It 's not a class, but let's put it here. Recently I want to change an ASP Web site to PHP, helpless space does not support MySQL database, had to use Access database, but used to php+mysql,ph ...

  • 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.