Home  >  Article  >  Backend Development  >  PHP access data connection and implementation code for reading, saving and editing data

PHP access data connection and implementation code for reading, saving and editing data

高洛峰
高洛峰Original
2016-12-30 14:20:001190browse

$conn = new com("ADODB.Connection"); 
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("www.jb51.net/db.mdb"); 
//与access连接要用到com接口了。 
$conn->Open($connstr); 
$rs = new com("ADODB.RecordSet"); 
//数据查询并显示出来 
$rs->Open("select * from szd_t",$conn,1,1); 
while(! $rs->eof) { 
$f = $rs->Fields(1); 
echo $f->value; 
$rs->MoveNext(); 
} 
//下面来看一下php access数据库教程保存 
$sql ="insert into szd_t(title)values('www.jb51.net')"; 
$rs->Open( $sql ); 
echo '保存成功'; 
//php access数据库编辑 
$sql ="Update szd_t set title='jb51.net' where id=".$_GET['id']; 
$rs->Open( $sql ); 
echo '编辑成功'; 
//删除数据 
$sql ="delete from szd_t where id=".$_GET['id'];

Three ways for php to connect to Access database
Recently I want to change an asp website to php, but unfortunately the space does not support mysql database, so I have to use access database, but in the past I used php+mysql. I have never done php+access database programming.
Thank you to the party, thank you to cctv, and thank you to the search engine. I couldn’t find a good article here, so I will share it with you.
The steps to connect Access in PHP are as follows 3 ways.
(1) To create a system data source, use the ODBC function provided by PHP.
(2) You can also use PHP's ODBC function, but do not create a data source.
Open Database Connection (Open DateBase Conection, ODBC) is one of the Windows Open Server (Open Services) API (WOSA) products. A data source is a named connection to a database. For different types of databases that the application wants to connect to, an ODBC driver is required. The ODBC API is primarily designed for client/server RDBMS use, but the ODBC driver can also be used to connect to desktop database files, worksheets, and flat files. ODBC uses the Odbcinst.dll library to set up and clear data sources. Odbcad32.exe is an independent 32-bit executable application for establishing ODBC data sources. It has its corresponding icon in the Control Panel.
The ODBC driver manager opens the ODBC driver for the data source and transmits 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 introduces how PHP uses ODBC to connect to the Access database. Use $connstr="DRIVER= Microsoft Access Driver (*.mdb) to set the data driver, and the function realpath() is used to obtain the relative path of the database. Using this method to connect to the Access database mainly applies to PHP's odbc_connect() function. This function The statement is as follows: www.phperz.com

resourse odbc_connect( string dsn, string user, string password [, int cursor_type]) 
dsn:系统dsn名称。 
user:数据库服务器某用户名。 
password:数据库服务器某用户密码。 
cursor_type:游标类型。

The code is 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 for Microsoft open database applications. Technology. It is designed to work with the new data access layer OLE DB Provider to provide universal data access (Universal Date Access), which is a low-level data access interface that can be used to access various data sources, including Traditional relational databases, email systems and customized 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, ADO is a high-level access technology. PHP Programmer Station
ADO technology is based on the Common Object Model (COM), which provides access technology in multiple languages. PHP uses the ADO method to manipulate the Access database by pre-defining the class COM. The details of this class are as follows: www. .phperz.com

string com::com( string module_name [, string server_name [, int codepage]]) 
module_name:被请求组件的名字或class-id。 www~phperz~com 
server_name:DCOM服务器的名字。 
Codepage:指定用于将PHP字符串转换成UNICODE字符串的代码页,反之亦然。该参数的取值有CP_ACP、CP_MACCP、CP_OEMCP、CP_SYMBOL、CP_THREAD_ACP、CP_UTF7和CP_UTF8。 

PHP利用com类并使用ADO方法访问数据库的代码如下: 
[code] 
$conn = new com("ADODB.Connection"); 
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("bookinfo.mdb "); 
$conn->Open($connstr);

This is an article posted by another netizen. In the end, Script House will provide a php+access guestbook source code, which you can basically refer to. The operation of php access is familiar.
Although PHP is rarely used to connect ACCESS, it is still good to guide data occasionally.

<?PHP 
/* 
创建ADO连接 
*/ 
$conn = @new COM("ADODB.Connection") or die ("ADO Connection faild."); 
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("DATUM/cnbt.mdb"); 
$conn->Open($connstr); 
/* 
创建记录集查询 
*/ 
$rs = @new COM("ADODB.RecordSet"); 
$rs->Open("select * from dbo_dirs",$conn,1,3); 
/* 
循环读取数据 
*/ 
while(!$rs->eof){ 
echo "$rs->Fields["title"]->Value; 
echo "<br/>"; 
$rs->Movenext(); //将记录集指针下移 
} 
$rs->close(); 
?>

Function description and example
Although PHP is rarely used. Link ACCESS, but occasionally used to guide data, it is quite good

For more articles related to the implementation code of php access data connection and reading, saving and editing data, please pay attention to the PHP Chinese website

!
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn