php链接mysql服务器地址_PHP 连接 Mysql 服务器的三种方式

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.

PHP 提供了三种 API 来连接 Mysql 数据库,下面展示的就是由三种扩展(1.mysql; 2.mysqli; 3.PDO)提供的 连接API。

每一个代码片段都创建了一个到服务器的连接,域名为"example.com",用户名为"user",密码"password",每一个执行都向用户打了一声招呼。

// mysqli

$mysqli = new mysqli("example.com", "user", "password", "database");

$result = $mysqli->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");

$row = $result->fetch_assoc();

echo htmlentities($row['_message']);

// PDO

$pdo = new PDO('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);

echo htmlentities($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);

echo htmlentities($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%.

推荐的使用的 API

推荐使用 mysqli 或者是 PDO_MySQL 扩展。对于新开发的项目来说,不推荐使用旧的 mysql 扩展,因为其在 PHP 的5.5.0 版中,已经被弃用了,而在 PHP7 中直接被移除了。下面提供了一个详细的功能对比表格。三个扩展的总体性能都差不多。在一个 PHP web 请求的运行时间内,扩展所占的影响只有非常小的一部分,差不多为 0.1%。

Itemext/mysqliPDO_MySQLext/mysqlPHP version introduced5.05.12.0

Included with PHP 5.xYesYesYes

Included with PHP 7.xYesYesNo

Development statusActiveActiveMaintenance only in 5.x; removed in 7.x

LifecycleActiveActiveDeprecated in 5.x; removed in 7.x

Recommended for new projectsYesYesNo

OOP InterfaceYesYesNo

Procedural InterfaceYesNoYes

API supports non-blocking, asynchronous queries with mysqlndYesNoNo

Persistent ConnectionsYesYesYes

API supports CharsetsYesYesYes

API supports server-side Prepared StatementsYesYesNo

API supports client-side Prepared StatementsNoYesNo

API supports Stored ProceduresYesYesNo

API supports Multiple StatementsYesMostNo

API supports TransactionsYesYesNo

Transactions can be controlled with SQLYesYesYes

Supports all MySQL 5.1+ functionalityYesMostNo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值