PHP Learning (iv)

Source: Internet
Author: User

Database

Prior to PHP5, the recommended way is to use native drivers such as Php_mysql.dll/php_psql.dll.

For example, Php_mysql.dll's syntax is as follows:

First, set the page encodingHeader"Content-type:text/html; Charset=utf8");Second, establish a database connection$conn=Mysql_connect ("LocalHost","Root","Root")OrDie"Database connection Failed");Create a databaseIfmysql_query (The CREATE DATABASE xxx",$con)) { Echo"The database was created successfully.";}else { Echo"Failed to create database:".Mysql_error ()."<br>";}Switch to Databasemysql_select_db ("XXX")OrDir"The clock-in database failed.");Set UTF8 encodingmysql_query ("Select Names UTF8");Create a table$sql=The CREATE TABLE person(ID int primary KEY auto_increment,Name varchar () is not NULL,Salary float Default 1500)";mysql_query ($SQL);Inserting datamysql_query (INSERT into person (name, salary) values (' Zhang Fei ', 3333)");mysql_query (INSERT into person (name, salary) VALUES (' Guan Yu ', 3334)");Querying dataReturns the result: a universal array$res=mysql_query ("SELECT * FROM person");Displaying data, using the Foreach LoopEcho"<table>\ nforeach ( $res as  $r) { echo  "<tr><td>< Span class= "Pl-smi" > $r [name]</td><td> $r [ Salary]</td></tr>\n "} echo  "\n </table>//Finally, release the connection  Mysql_close ( $conn);             

Because the MySQL plugin has a lot of problems, so, then the php_mysqli.dll plug-in, which I represents improvement (improve/upgrade) meaning, to connect the database efficiency and security of a lot of optimization.

In addition to fully compatible with MySQL syntax, the MYSQLI plugin supports the use of OO connections to databases:

Connecting to a database$conn=NewMysqli ("LocalHost","Root","Root",The text");$conn, Query ("Set names UTF8");Inquire$sql=$conn->query ("SELECT * FROM person");Get results$res=$conn->fetch_array (MYSQLI_ASSOC);Show resultsEcho"<table>\n " echo  "<tr><td>" Span class= "Pl-k".  $res [ ' Name "  "</td><td>"  $res [ ' Salary "  "</td></tr><br>" echo  "</table>";    

Although the Mysqli plugin has been very useful. But...

PHP can connect to the database can not only MySQL ah, there are oracle/sqlserver/postgres/sqlite, and so on, each database has its own plug-ins, have their own syntax ... So later, PHP introduced its own database connection standard, the PDO (PHP Data Object) interface, the database connection is standardized. So, with all the databases of DPO, the syntax is unified:

Connecting to a database$pdo=NewPDO ("Mysql:host=localhost;dbname=test","Root","Root",The test");Operational Database N$pdo->exec ("Set names UTF8");$pdo->exec ("CREATE TABLE Person ...")OrDie"Creation failed");$pdo->exec ("INSERT INTO person ...")Echoand (The insert succeeds");Query$st=$pdo->query ("Select xxx from yyy");$rows=$st->fetch (Pdo::FETCH_ASSOC);Print_r ($rows);Operation using the Prepare method$stmt=$pdo->prepare ("Select name from the person where id =: ID");$stmt->bindparam ( $xxx, pdo::param_int);   $stmt =  $stmt ->execute (); Span class= "pl-s1" > $rows =  $stmt - >fetchall (pdo::FETCH_ASSOC);  print_r ( $rows); //release link  $pdo Span class= "Pl-k" >= null;            

PHP Learning (iv)

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.