Jump to content

variables into pdo query without binding


niche
 Share

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

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

  

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...