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
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.

Link to comment
Share on other sites

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. 

Edited by Strider64
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.