PDO 和 MySQLi 准备语句有什么区别? [英] What is the difference between PDO and MySQLi prepared statements?

查看:53
本文介绍了PDO 和 MySQLi 准备语句有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个prepared语句有什么区别?

What is the difference between these two prepared statements?

$stmt = $pdo->prepare('SELECT * FROM employees WHERE name = :name');

$stmt->execute(array('name' => $name));

foreach ($stmt as $row) {
    // do something with $row
}

2

$stmt = $dbConnection->prepare('SELECT * FROM employees WHERE name = ?');
$stmt->bind_param('s', $name);

$stmt->execute();

$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    // do something with $row
}

我检查了很多关于准备好的语句的课程,但我唯一理解的是第二种方式,因为它可以用程序编写,这不是和 PDO 一样吗?因为它们都是准备好的语句?它们之间有什么速度差异或易用性吗?我学习了第二种方式,因为我认为 PreparedStatment = PDO 但当我知道它不是 PDO 时我感到震惊,使用

i checked many courses about prepared statements but the only one i understood was the 2nd way, since it could be written in procedural, Isn't it the same as PDO? since both of them are Prepared statements? Is there any speed difference or ease of use between them? I learnt the 2nd way because i thought PreparedStatment = PDO but i was shocked when i knew that it is not PDO, using

mysqli_prepare
mysqli_stmt_bind_param
mysqli_stmt_execute
mysqli_stmt_bind_result

推荐答案

区别如下:-

  1. Mysqli 仅用于 MySQL 数据库.PDO 支持使用相同功能的其他数据库.

  1. Mysqli is only for the MySQL database. PDO supports other database using the same functions.

Mysqli 既可以用于面向对象的风格,也可以用于过程风格.PDO 始终是面向对象的.

Mysqli can be used in either an object-oriented style or a procedural style. PDO is always object-oriented.

Mysqli 支持带有 ? 参数占位符的预处理语句.PDO 支持 ? 占位符和命名占位符,如 :columnName.

Mysqli supports prepared statements with ? placeholders for parameters. PDO supports both ? placeholders and also named placeholders, like :columnName.

Mysqli 要求您使用函数将每个参数值绑定到准备好的语句.PDO 还允许您在执行准备好的语句时简单地传递参数值数组.

Mysqli requires that you use a function to bind each parameter value to the prepared statement. PDO also allows you to simply pass an array of parameter values as you execute the prepared statement.

这篇关于PDO 和 MySQLi 准备语句有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆