Jump to content
Sign in to follow this  
niche

variables into pdo query without binding

Recommended Posts

Thought I could alter a table with a pdo query and binding. Apparently not.

Is there a way to use a pdo query to alter a table without binding using some kind of variable?  if so, what would a simple example look like?

Else, I'll use mysqli.   

Edited by niche

Share this post


Link to post
Share on other sites

Without a binding? You might be able to use a prepared query like this.

<?php
$stmt = $dbh->prepare("SELECT * FROM REGISTRY WHERE name = ? AND id = ?");
if ($stmt->execute(array($name, $id))) {
  //Stuff
}
?>

If you want to see the other ways (mostly binding) you can check it out on the doc page:
http://php.net/manual/en/pdo.prepared-statements.php

 

Although I'm perplexed as to why it is imperative to not use a binding.

Edited by Funce

Share this post


Link to post
Share on other sites

Certain elements in queries can't have placeholders, like the LIMIT clause. I haven't tested, but it's probable that ALTER can't have placeholders either since MySQL needs to know the table structure when preparing queries because it uses the data types of the fields to determine how to sanitize the data. If you find yourself having to give the user the ability to alter tables then your database structure may be badly designed.

If it is important that you alter tables with variables in the query, you can just put the variables into the SQL string itself, making sure to have carefully sanitized them beforehand.

Share this post


Link to post
Share on other sites
Quote

 then your database structure may be badly designed

Not badly designed (yet).  Bad approach!

Many thanks to the fox and Funce.  

Found this link and learned more about PDO investigating PDO rules.

https://stackoverflow.com/questions/9250434/pdo-quote-method

  

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.   Paste as plain text instead

  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.

Loading...
Sign in to follow this  

×
×
  • Create New...