PHP Access data connection and read save Edit data implementation code _php tips

Source: Internet
Author: User
Tags driver manager dsn odbc object model ole access database
Copy Code code as follows:

$conn = new COM ("ADODB. Connection ");
$connstr = "Driver={microsoft Access DRIVER (*.mdb)}; Dbq= ". Realpath ("Www.jb51.net/db.mdb");
The COM interface is used to connect to access.
$conn->open ($CONNSTR);
$rs = new COM ("ADODB. RecordSet ");
Data query and display it
$rs->open ("SELECT * from szd_t", $conn, 1, 1);
while (! $rs->eof) {
$f = $rs->fields (1);
Echo $f->value;
$rs->movenext ();
}
Let's take a look at the PHP Access database tutorial Save
$sql = "INSERT into szd_t (title) VALUES (' www.jb51.net ')";
$rs->open ($sql);
Echo ' Save success ';
PHP Access database editing
$sql = "Update szd_t set title= ' jb51.net ' where id=". $_get[' id '];
$rs->open ($sql);
Echo ' Edit succeeded ';
Delete data
$sql = "Delete from szd_t where id=". $_get[' id '];


PHP three ways to connect to an Access database
Recently want to change an ASP's website into PHP, helpless space does not support MySQL database, had to use Access database, but used to php+mysql,php+access database programming has not really done.
Thank the party, thanks to CCTV, thanks to the search engine, this does not find a good article, special turned around and share.
There are 3 ways to connect to access in PHP.
(1) Create the system data source, with the ODBC function provided by PHP.
(2) You can also use PHP's ODBC functions, but do not create a data source.
Open Datebase Conection,odbc is one of the windows open Server (open service) API (WOSA) products. A data source is a named connection to a database. An ODBC driver is required for the different types of databases that the application will connect to. The ODBC API is designed primarily for use with client/server RDBMS, 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 building an ODBC data source, with its corresponding icon control Panel in the dashboard.
The ODBC Driver Manager opens the ODBC driver for the data source and sends the SQL statement to the driver. After a client/server RDBMS has finished 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. Phperz.com
The following is a description of how PHP connects to an Access database using ODBC. Use $connstr= "driver= Microsoft Access DRIVER (*.mdb) to set data-driven, function Realpath () to get the relative path of the database. Using this method to connect an Access database is primarily applied to PHP's Odbc_connect () function, which is declared as follows: www.phperz.com
Copy Code code as follows:

Resourse odbc_connect (String dsn, string user, string password [, int cursor_type])
DSN: System DSN name.
User: The database server has a username.
Password: Database Server a user password.
Cursor_type: Cursor type.

The code is as follows:
Copy Code code as follows:

$connstr = "Driver=microsoft Access DRIVER (*.mdb);
Dbq= ". Realpath (" Bookinfo.mdb ");
$connid =odbc_connect ($connstr, "", "", SQL_CUR_USE_ODBC);

(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 in conjunction with the new data Access layer OLE DB provider, providing universal data access (Universal Date access). OLE DB is a low-level data provider that can access 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
ADO technology is based on the Common object Model (COM), it provides the access technology of many languages. PHP uses the ADO method to manipulate an Access database by defining class COM in advance. The class details are as follows: www.phperz.com
Copy Code code as follows:

String com::com (String module_name [, String server_name [, int codepage]])
Module_name: The name or Class-id of the requested component. Www~phperz~com
Server_name:dcom the name of the 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.

PHP uses the COM class and uses the ADO method to access the database code as follows:
[Code]
$conn = new COM ("ADODB. Connection ");
$connstr = "Driver={microsoft Access DRIVER (*.mdb)}; Dbq= ". Realpath ("Bookinfo.mdb");
$conn->open ($CONNSTR);


This is an article written by other netizens. Combined, the last cloud habitat community will give a php+access message source code, we can refer to the next. Basically, you're familiar with the operation of PHP access.
Although it's very rare to use PHP to link to access, it's pretty good to guide data occasionally.
Copy Code code as follows:

? Php
/*
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);
/*
To create a recordset query
*/
$rs = @new COM ("ADODB. RecordSet ");
$rs->open ("SELECT * from Dbo_dirs", $conn, 1, 3);
/*
Looping through data reading
*/
while (! $rs->eof) {
echo "$rs->fields[" title "]->value;
echo "<br/>";
$rs->movenext (); Move the recordset pointer down
}
$rs->close ();
?>


function description and examples
Although it's very rare to use PHP to link to access, it's pretty good to guide data occasionally.

PHP ACCESS Simple Message This example 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.