Jump to content

PHP pdo update, which one is better?


colap

Recommended Posts

$sql = "UPDATE books 
        SET title=?, author=?
		WHERE id=?";
$q = $conn->prepare($sql);
$q->execute(array($title,$author,$id));
UPDATE `access_users` 
SET `contact_first_name` = :firstname,
`contact_surname` = :surname,
`contact_email` = :email,
`telephone` = :telephone 
WHERE `user_id` = :user_id

Which one is better to follow and why is that better?

Link to comment
https://forums.phpfreaks.com/topic/297675-php-pdo-update-which-one-is-better/
Share on other sites

You mean which style is better, quotation marks or named placeholders? There's no difference technically, but if you use named placeholders then it's easier to make sure your variables are lined up correctly and less risk of accidentally putting variables in the wrong place.

What I do is use placeholder names then try to keep everything constant (prepared statements, variables and what have you).

 

for example

$query = "UPDATE users SET username=:username WHERE id=:id";
$stmt = $pdo->prepare($query);
$result = $stmt->execute([':username' => $_POST['username'], ':id' => $_POST['id']]); 

That way is easier to spot a syntax error easier in my opinion. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.