api connect like.php,PHP - Manual: Choosing an API (官方文档)

Choosing an API

PHP offers three different APIs to connect to MySQL. Below we show

the APIs provided by the mysql, mysqli, and PDO extensions. Each code snippet

creates a connection to a MySQL server running on "example.com" using

the username "user" and the password "password". And a query is run to

greet the user.

Example #1 Comparing the three MySQL APIs

<?php // mysqli$mysqli= newmysqli("example.com","user","password","database");$result=$mysqli->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");$row=$result->fetch_assoc();

echohtmlentities($row['_message']);// PDO$pdo= newPDO('mysql:host=example.com;dbname=database','user','password');$statement=$pdo->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");$row=$statement->fetch(PDO::FETCH_ASSOC);

echohtmlentities($row['_message']);// mysql$c=mysql_connect("example.com","user","password");mysql_select_db("database");$result=mysql_query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");$row=mysql_fetch_assoc($result);

echohtmlentities($row['_message']);?>

Recommended API

It is recommended to use either the mysqli

or PDO_MySQL extensions.

It is not recommended to use the old mysql

extension for new development, as it was deprecated in PHP 5.5.0 and was

removed in PHP 7. A detailed feature comparison matrix is provided below.

The overall performance of all three extensions is considered to be about

the same. Although the performance of the extension contributes only a

fraction of the total run time of a PHP web request. Often, the impact is

as low as 0.1%.

Feature comparison

ext/mysqli

PDO_MySQL

ext/mysql

PHP version introduced

5.0

5.1

2.0

Included with PHP 5.x

Yes

Yes

Yes

Included with PHP 7.x

Yes

Yes

No

Development status

Active

Active

Maintenance only in 5.x; removed in 7.x

Lifecycle

Active

Active

Deprecated in 5.x; removed in 7.x

Recommended for new projects

Yes

Yes

No

OOP Interface

Yes

Yes

No

Procedural Interface

Yes

No

Yes

API supports non-blocking, asynchronous queries with mysqlnd

Yes

No

No

Persistent Connections

Yes

Yes

Yes

API supports Charsets

Yes

Yes

Yes

API supports server-side Prepared Statements

Yes

Yes

No

API supports client-side Prepared Statements

No

Yes

No

API supports Stored Procedures

Yes

Yes

No

API supports Multiple Statements

Yes

Most

No

API supports Transactions

Yes

Yes

No

Transactions can be controlled with SQL

Yes

Yes

Yes

Supports all MySQL 5.1+ functionality

Yes

Most

No

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值