Jump to content
Sign in to follow this  
colap

PHP pdo update, which one is better?

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?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

It doesn't matter at all personally i use the placeholder names as if you dynamically create the queries its much less likely something will go wrong

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

I really see no reason to use ? placeholders. It makes it hard to read and on more complicated queries it just becomes a headache very fast.

Share this post


Link to post
Share on other sites

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.

Sign in to follow this  

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