What is the difference between PDO's query() vs execute() ?
What is the difference between PDO's query() vs execute() ?
Certainly! Understanding the difference between PDO::query()
and PDO::execute()
is crucial for working with databases in PHP. Here's a detailed explanation:
query()
method is used to execute an SQL statement directly and returns a result set as a PDOStatement
object.SELECT
, SHOW
, or DESCRIBE
.PDOStatement
object on success or false
on failure.$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password');
$stmt = $pdo->query('SELECT * FROM users');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
print_r($row);
}
execute()
method is used to execute a prepared statement. It is called on a PDOStatement
object that has been prepared using the prepare()
method.true
on success or false
on failure.$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password');
$stmt = $pdo->pre...
middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào