PHP access data connection and read save edit data implementation code

  • 2020-03-31 20:47:04
  • OfStack

 
$conn = new com("ADODB.Connection"); 
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("www.jb51.net/db.mdb"); 
//The com interface is needed to connect with access.
$conn->Open($connstr); 
$rs = new com("ADODB.RecordSet"); 
//The data is queried and displayed
$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 success '; 
//Delete the data
$sql ="delete from szd_t where id=".$_GET['id']; 


Three ways PHP can connect to an Access database
Recently want to change the website of an asp to PHP, but the space does not support mysql database, had to use access database, but before is to use PHP +mysql, PHP +access database programming has really not done.
Thanks to the party, thanks to CCTV, thanks to the search engine, not to find a good article, especially turn to share with you.
There are three ways to connect Access in PHP.
(1) create the system data source, using the ODBC function provided by PHP.
(2) you can also use PHP's ODBC function without creating a data source.
Open DateBase Conection (ODBC) is one of the Windows Open Server API (WOSA) products. A data source is a named connection to a database. An ODBC driver is required for the different types of databases to which the application is connecting. The ODBC API is primarily designed for use by the RDBMS of the client/server, 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 a standalone 32-bit executable application for establishing ODBC data sources, with its corresponding icon Control Panel in the Control Panel.
The ODBC driver manager opens the ODBC driver for the data source and passes the SQL statement 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
Here's how PHP USES ODBC to connect to an Access database. Use $connstr="DRIVER= Microsoft Access DRIVER (*.mdb) to set the data DRIVER, the function realpath() to get the relative path of the database. Using this method to connect to an Access database is mainly applied to PHP's odbc_connect() function, which is declared as follows: www.phperz.com
 
resourse odbc_connect( string dsn, string user, string password [, int cursor_type]) 
dsn : the system dsn The name.  
user : user name of database server.  
password : user password of database server.  
cursor_type : 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) 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 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 program, so ADO is a high-level access technology. PHP programmer station
ADO technology is based on the general object model (COM), which provides access to many languages. PHP USES the ADO method to manipulate the Access database by pre-defining the COM class. This class is detailed as follows: www.phperz.com
 
string com::com( string module_name [, string server_name [, int codepage]]) 
module_name : the name of the requested component or class-id .  www~phperz~com 
server_name : DCOM The name of the server.  
Codepage : specified for use by PHP String conversion to UNICODE String code page and vice versa. The value of this parameter is CP_ACP , CP_MACCP , CP_OEMCP , CP_SYMBOL , CP_THREAD_ACP , CP_UTF7和CP_UTF8 .  

PHP using com Class and use 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 posted by another netizen. Combined, finally this site will give a PHP +access message source code, you can refer to the next. Basically, you are familiar with the operation of PHP access.
While PHP is rarely used to link to ACCESS, it is nice to use it occasionally to guide derivative data
 
<?PHP 
 
$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(); //Moves the recordset pointer down
} 
$rs->close(); 
?> 


Function description and examples
While PHP is rarely used to link to ACCESS, it is nice to use it occasionally to guide derivative data

(link: #)

Related articles: