问题
What is the difference between these two prepared
statements?
1
$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
}
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:
The difference is below:-
Mysqli
is only for the MySQL database.PDO
supports other database using the same functions.Mysqli
can be used in either an object-oriented style or a procedural style.PDO
is always object-oriented.Mysqli
supports prepared statements with?
placeholders for parameters.PDO
supports both?
placeholders and also named placeholders, like:columnName
.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.
来源:https://stackoverflow.com/questions/42778166/what-is-the-difference-between-pdo-and-mysqli-prepared-statements