When I started to contact PHP, really feel its charm, in the attitude of learning and breaking the inherent language and mode of thinking, began a trip to PHP, in general, go still relatively smooth, in which can see C,java,perl shadow, learning curve is not big, But a good product still has a long way to do.
Needless to say, slowly feeling and understanding, this article mainly about PHP operation database three kinds of extensions.
Three ways to extend PHP access to a database:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/47/DD/wKioL1QBkeWAA_GrAAD2LHLHPDQ256.jpg "title=" Php-mysql.png "width=" 680 "height=" 228 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:680px;height:228px; "alt=" Wkiol1qbkewaa_graad2lhlhpdq256.jpg "/>
Here are three ways to access the database and query the instance code of the data:
1.mysql expansion
<?PHP//1: Get Database connection $connection = @ mysql_connect (' 127.0.0.1:3306 ', ' root ', ' root ') or die (' mysql connection failed ' . mysql_error ());//2: Select Database mysql_select_ db (' Phptest ') or die (' database phptest not exist ' . mysql_error ());// 3: Query data $sql = ' select id as ' number ', name as ' name ', birthday as ' Date of birth ', phone as ' contact information ' , address as ' address ' ' . ' from ' t_user ' limit 0 , 30 '; $result = mysql_query ($sql) or Die (' query failed ' . mysql_error ());//4: Processing Query Results # Print query data echo ' <table> '; #打印列信息 $i = 0;echo ' <tr> ';while ($i < mysql_num_fields ($result)) {$meta = mysql_fetch_field ($result);echo ' <td> ';echo $meta->name;echo ' </td> '; $i + +;} echo ' <tr/> '; #打印Line Records while ($line = mysql_fetch_array ($result, &NBSP;MYSQL_ASSOC)) {echo ' <tr> '; foreach ($line as $value) {echo ' <td> ' . $value . ' </td > ';} echo ' </tr> ';} echo ' </table> ';//5: Release result memory Mysql_free_result ($result);//6: Close database connection mysql_close ($connection);? >
2.mysqli expansion
<?php# use an object-oriented approach to MySQL database//1: Create Mysqli object $mysqli = new mysqli ();//2: Connect to Database $mysqli->connect (' 127.0.0.1 ', ' root ', ' root '; $mysqli->select_db (' phptest ');if ($mysqli->connect_errno) {echo ' failed to connect database ';exit ();} 3: Get mysqli_stmt (Execute SQL command) object, $stmt $sql = ' select id as ' number ', name as ' Name ', birthday as ' date of birth ', phone as ' contact us ' , address as ' address ' ' . ' from ' t_user ' limit 0 , 30 ';if ($stmt = $mysqli->prepare ($sql ) {#执行查询 $stmt->execute () $stmt->bind_result ($id, $name, $birthday, $phone, $ #打印查询数据echo ' <table> '; #打印列信息echo ' <tr> '; $meta = $stmt->result_ Metadata ()->fetch_fields ();foreach ($meta as $val) {echo ' <td> '; echo $ val->name;echo ' </td> ';} echo ' <tr/> ';while ($stMt->fetch ()) {echo ' <tr> ';echo ' <td> ' . $id . ' </td> '; echo ' <td> ' . $name . ' </td> ';echo ' <td> ' . $ birthday . ' </td> ';echo ' <td> . $phone . ' </td> '; echo ' <td> ' . $address . ' </td> ';echo ' </tr> ';} echo ' </table> ';echo ' The above uses the Mysqli extended object-oriented approach to database operations, and how to use a process-oriented style manipulation database requires the use of functions. For example, close the database connection, object-oriented style is: $mysqli->close () and the use of the process-oriented style is: Close ($mysqli), details can be see php_manual.
3.pdo_mysql extension
Database connection configuration information pdo-inc.php:
<?php $dsn = ' mysql:host=127.0.0.1:3306;dbname=phptest '; $username = ' root '; $password = ' root '; $opt = Array (PDO:: ATTR _timeout = 40);? >
Accessing a database using PDO
<?PHP//1: Introduction of database connection Information require_once (' pdo-inc.php ');//2: Create PDO object try {$pdo = new pdo ($dsn , $username, $password, $opt);} catch (pdoexception $e) {echo ' database connection failed ' . $e->getmessage (); exit ();} $sql = ' select id as ' number ', name as ' name ', birthday as ' Date of birth ', phone as ' contact information ' , address as ' address ' ' . ' from ' t_user ' limit 0 , 30 ';//3: Create Precompiled Command Object pdostatementif ($stmt = $pdo->prepare ($sql)) &NBSP;{//4: Execute $stmt->execute (); #打印查询数据echo ' <table> ';echo ' < Tr> ';for ($i = 0, $columnCount = $stmt->columncount (); $i < $columnCount; $i + +) {$meta = $stmt->getcolumnmeta ($i);echo ' <td> '; echo $meta [' name '];echo ' </td> ';} echo ' <tr/> '; $row _num = 0;while ($row = $stmt->fetch (pdo :: fetch_num) {$row _num++;echo ' <tr> ';foreach ($row as $value) {echo ' <td> ' . $value . ' </td > ';} echo ' </tr> ';} echo ' </table> '; #需要统计查询的行数echo ' altogether ' . $row _num . ' record <br/> Use Pdostatement::rowcount to count, Total ' . $stmt->rowcount () . ' record ';//5: Close cursor $stmt-> Closecursor ();} 6: Release PDO object $pdo = null;? >The obvious difference between seeing the above code is that accessing the MySQL database, and no longer seeing MySQL-related functions and typefaces, is what PDO wants, and PDO makes PHP access to the database interface-oriented rather than concrete implementations, This also provides the basis for changing the database without worrying about the excessive coupling of the application to the database. In short, the principle of interface-oriented programming in object oriented is achieved.
In terms of operational data, more of the API to programming, with the knowledge of the database (mainly SQL) and familiar with the relevant API can operate the database, in the face of PHP operation of the database of various extensions, in the end use which way, temporarily do not know, see the situation.
This article from "Mustang Red" blog, declined reprint!
[PHP] PHP programming three ways to operate the MySQL database