PHP database drive, connect data different ways to learn notes _php tutorial

Source: Internet
Author: User
Tags dbx ibm db2 odbc connection php and mysql php database table definition access database openldap
Table of Contents 1. Introduction to PHP Database Driver 2. PHP connects to the database in different ways 1. PHP Database Driven Introduction driver is a piece of software code designed to interact with a particular type of database server. Some libraries may be called by the driver. Copy code 1 similar to the database-driven concept in Java. JDBC-ODPC Bridge: It maps the JDBC API to the ODPC API. Let JDBC-ODPC call the database local driver code (that is, the database vendor provides database operations binary code libraries, such as Oci.dll in Oracle) 2. The native API driver maps the JDBC API directly to a database-specific client API that loads the local code base (c + +, etc.) provided by the database vendor through the client 3. Network protocol-driven (mainstream) This type of driver provides a network API to the client, and the JDBC driver on the client uses a socket (socket) to invoke the middleware program on the server, which translates its request into the specific API call required. 4. Local protocol-driven (mainstream) This type of driver uses the socket to communicate directly between the client and the database. It is a direct interaction with the database instance of the JDBC driver is intelligent, it knows the database use of the underlying protocol, is also the most mainstream use of the JDBC driver, our focus of this chapter is that it copy the code and for PHP, the same mainstream use is the network protocol driver, local protocol driver, MySQL client library, MySQL native driver library. These libraries implement the underlying protocol for interacting with the MySQL database server. Database drivers are at the very bottom of the communication between PHP and the database, and different database vendors implement their own drivers based on a framework that provides basic functionality and advanced functionality for a particular database. Above the driver layer is the "connector", or the adapter abstraction layer, for PHP code and database connection, the programmer can use PDO (PHP Database Object), or directly using the extension interface (MySQL, mysqli) These exposed APIs communicate with the underlying database. Database vendor-provided underlying database driver mysql: downloads/index.html#databasesqlserver: (SQL.90). aspx ... File-based databaseFile type is a file-based database engine that uses file I/O (input/output) functions to store and read databases from files on disk. It is also generally much smaller than relational databases (such as MySQL) (for example, the typical file-type Database SQLite command-line version is less than 200KB in size), while the file-type database supports most of the SQL commands you are familiar with, while having easy-to-carry features next, Let's start with the big picture above and learn the different ways PHP connects to the database, and their pros and cons in different business scenarios 2. Different ways for PHP to connect to a database 0x1: Using the extended API interface to communicate with the database PHP code is made up of one core, and some optional extensions make up the core functionality. MySQL-related extensions for PHP, such as Mysqli,mysql, are implemented based on the PHP extension framework. An extension of a typical function is to expose an API to a PHP programmer, allowing the extension of its own functionality to be used by programmers. Of course, there are also a subset of extensions that are developed based on the PHP extension framework that will not expose the API interface to PHP programmers. For example, the PDO MySQL driver extension does not expose the API interface to a PHP programmer, but it provides an interface to the PDO layer on its upper level. For the writing of PHP extensions see another blog post in practical programming, the most frequently used or extended API is to connect to the database extension=php _mysql.dll This is a design development that allows PHP applications to interact with the MySQL database in an early extension. The MySQL extension provides a process-oriented interface and is designed for MySQL4.1.3 or earlier versions. Therefore, although this extension can interact with the MySQL4.1.3 or the updated database server, it does not support some of the features provided by the late MySQL server. mysql extension source code is copied under PHP extension directory Ext/mysql \ n "; while ($line = Mysql_fetch_array ($result, MYSQL_ASSOC)) {echo "\ t\ n "; foreach ($line as $col _value) {echo "\t\t$col _value\ n "; } echo "\ t\ n "; } echo "\ n "; Release result set Mysql_free_result ($result); Close connection mysql_close ($link);? > Copy Code extension=php_mysqli.dll mysqli extensions, which we sometimes call MySQL enhanced extensions, can be used to use the new advanced features in MySQL4.1.3 or later versions. The mysqli extension is included in PHP 5 and later. The mysqli extension has a number of advantages, as compared to the MySQL extension, the main improvements are: Copy code 1. Object-oriented Interface 2. Prepared statement support (that is, parameter compilation preprocessing, can effectively prevent the occurrence of SQL injection) 3. Multi-statement execution support 4. Transaction support 5. Enhanced debugging Capabilities 6. Embedded Services Support 7. It provides a process-oriented interface while providing an object-oriented interface. The copy code mysqli extension is built using the PHP extension framework, and its source code is copied from the Ext/mysqli in the PHP source directory Prepare ($sql); $name = $_get[' name ']; $pass = $_get[' pass '); Add parameters to SQL query $cmd->bind_param ("ss", $name, $pass); $cmd->execute (); $cmd->bind_result ($result); $cmd->fetch (); if ($result) {var_dump ($result);}? > Copy Code mysqli In addition to using parameter compilation preprocessing for database communication, but also compatible with process-oriented code replication Copy code PHP also supports many other database connection extensions, using similar methods, as long as you follow the function call specification, for more details see 0x2: Using the PDO abstraction layer to communicate with the database PDO (PHP Data Objects php object) is a Database abstraction layer specification in PHP applications. PDO provides a unified API interface that allows your PHP application to not care about the type of database server system you want to connect to. In other words, if you use the PDO API, you can seamlessly switch the database server whenever you need it, such as from Firebird to MySQL, just to modify very little PHP code. Examples of other database abstraction layers include JDBC in Java applications and DBI in Perl. Note: Using PDO to extend itself does not implement any database functionality; You must use the PDO driver of a specific database to access the database service (it is just an interface specification) but conversely, the more compatibility an interface provides, the weaker its customization and specificity (which is easy to understand), The main disadvantage of the PDO interface API is that it restricts you from using the MySQL server to provide all the database advanced features. For example, PDO does not allow multi-statement execution with MySQL support. In PHP5, PDO currently supports a large number of databases and will be used as the default database connection in PHP6:1. Sqlite2. Mysql3. Pgsql4. MSSQL ... PDO is based on the PHP extension framework implementation, its source code in the PHP source directory under the Ext/pdo again, PDO is only an interface specification, it does not implement any database functions, programmers must use a specific database of "PDO driver" to access a specific database Extension=php_pdo_mysql.dll Copy Code ' SET NAMES UTF8 ',); $dsn = ' mysql:host= '. $dbhost. ';p ort=3306;dbname= '. $dbname; try {$dbhdl = new PDO ($DSN, $dbusr, $dbpwd, $opt);//display exception $dbhdl->setattribute (pdo::attr_errmode,pdo::er Rmode_exception); } catch (Pdoexceptsddttrtion $e) {//return pdoexception print "error!:". $e->getmessage (). "
"; Die (); } $dbstm = $dbhdl->query (' SELECT * from P8_ad_user LIMIT 0,1 '); $rows = $dbstm->fetchall (PDO::FETCH_ASSOC);//$rows = $DBHDL->fetch (); Print_r ($rows);? > Copy Code extension=php_pdo_pgsql.dll Copy code "; } catch (Exception $e) {echo "Unable to connect:". $e->getmessage (). "

"; } $DBH->begintransaction (); $query = "SELECT * from P8_ad_user LIMIT 0,1"; $DBH->query ($query); $query = "FETCH all in \" $cursor \ ""; echo "Begin data

"; foreach ($dbh->query ($query) as $row) {echo "$row [0] $row [1] $row [2]
"; } echo "End data";? > Copy code only mysql, PostgreSQL for example, in fact, PDO this abstraction layer way to access the majority of the current mainstream database, and PDO will become the default database connection PHP6, more details see /manual/zh/book.pdo.php 0x3: Using the ODBC abstraction layer to communicate with the database ODBC is an application programming interface (application programming Interface,api) that gives us the ability to connect to a data source ( An MS Access database, for example, attempts to standardize connection methods, such as functionality and configuration, through programming languages and database query Access (SQL Normalization). The role of ODBC is to act as an interface or connector, which has a dual design goal: 1. First, for an ODBC system, it acts as a programming language system 2. Second, for a data storage system, it acts as an ODBC system. Therefore, ODBC requires a "programming language for ODBC" driver (such as the PHP-ODBC Library) and a "ODBC for data Storage System" driver (such as the MYSQL-ODBC Library). In addition to the ODBC system itself, ODBC can handle the configuration of the data source, allowing ambiguity between the data source and programming language. Unlike the extended API interface, PDO, which was previously learned, it is a little cumbersome to use ODBC to connect to a database (at least personally), and this "hassle" is reflected in the need to configure the operating system of the target database server to create an ODBC data source, Then you can make an ODBC connection by creating an ODBC connection to the MS Access data: Copy code 1. In Control Panel, open Administrative Tools 2. Double-click the data Source (ODBC) icon in the 3. Select the System DSN tab 4. Click on the "Add" button in the System DSN tab 5. Select Microsoft Access Driver. Click Done 6. In the next screen, click "Select" to locate Database 7. Take a data source name (DSN) 8 for this database. Click OK to copy code: copy Code "; echo "Companyname"; echo "Contactname"; while (Odbc_fetch_row ($rs)) {$username = Odbc_result ($rs, "username"); $password = Odbc_result ($rs, "password"); echo "$username"; echo "$password"; } Odbc_close ($conn); echo "";? >Copy code 0x4: using DBX to communicate with database PHP itself has a DBX function built into it, and the DBX module is a Database abstraction layer (the "X" in DBX means the X database it can support). The DBX function allows you to access all of the DBX supported databases. DBX supports the following databases: 1. Mysql2. ODBC3. PgSQL4. Mssql (Microsoft SQL Server) 5. Fbsqldownload: Copy Code <title>A php-dbx URL Organizer</title> ERROR ADDING URL: ". Dbx_error ($dbconn); } else {print ("

$action: $url succeeded!

"); }} /*** MAIN ***/$dbconn = Dbx_connect ($MODULE, $server, $database, $user, $password) or Die ("Cannot connect to database"); >

PHP DBX URL Organizer

data) = = 0) {?>

Sorry, there is no URLs in the database. You should add some.

data); $i + +) {?>
Url Description
data[$i] [' url ']?>> data[$i] [' url ']?> data[$i] [' Description ']?> data[$i] [' url '])? >>delete
Copy code 0x5: using the DBA (Database (Dbm-style) abstraction layer) to communicate with the DB the DBA abstraction layer of PHP is used to support a file-type database such as Berkeley DB. These functions build the foundation for accessing Berkeley DB style Databases. In the Berkeley BSD series operating system, there is a simple database structure that forms an ultra-small database system with several files, a DBA database that forms an abstraction layer (abstraction layers). Currently the DBA database supported by PHP includes replication code 1. DBM: Berkeley's earliest DBA database NDBM: Newer and more resilient dba The DBAFTP://FTP.GNU.ORG/PUB/GNU/GDBM/4 of GDBM:GNU development. DB2: DB2 (non-IBM DB2) HTTP://WWW.OPENLDAP.ORG/LISTS/OPENLDAP-SOFTWARE/199905/MSG00009.HTML5 developed by Sleepycat Software. CDB: This is the qmail author developed a fast and reliable dba copy code after installing the DBA, use the following code to connect the replication code Copy Code 3. PostScript above is the PHP connection database different ways of learning, through this study, we learned that the current PHP development in the mainstream use of the connection database technology is 1. MySQL extension API2. Mysqli extension API3. PDO abstraction Layer The next thing you want to do 1. Consider the protocol-driven underlying principle of PHP and MySQL interaction 2. Try programming a simple communication protocol driver true techarticle Table of Contents 1. Introduction to PHP Database Driver 2. PHP connects to the database in different ways 1. PHP Database Driven Introduction driver is a design used to interact with a particular type of database server ...

  • Related Article

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