News for hackers. Stuff that matters.
Re:Up to 12M websites may have been compromised via Drupal v... by opendais on Saturday November 01, 2014 @02:20PM (#8544031)
You can write a function for it using ?'s being generated inside your code only. Its perfectly safe to do it that want and allows you to bind parameters.

The only explanation for not doing that is ignorance and/or incompetence. Mistakes happen but to claim its a language problem is incorrect.

http://php.net/manual/en/pdostatement.bindparam.php

<?php /* Execute a prepared statement by binding PHP variables */ $calories = 150; $colour = 'red'; $sth = $dbh->prepare('SELECT name, colour, calories FROM fruit WHERE calories < ? AND colour = ?'); $sth->bindParam(1, $calories, PDO::PARAM_INT); $sth->bindParam(2, $colour, PDO::PARAM_STR, 12); $sth->execute(); ?>

So what you do is you write a function to convert the array to a series of ?'s for the IN clause/tuple and then iterate through the array to bind the parameters.

Re:Up to 12M websites may have been compromised via Drupal v... by __david__ on Saturday November 01, 2014 @05:19PM (#8544656)
> You can write a function for it using ?'s being generated inside your code only. Its perfectly safe to do it that want and allows you to bind parameters.

Of course you can. Everyone here is saying you shouldn't have to. This is the perfect type of thing to move into the db library. That makes it much safer because you don't have to hand-roll the same stupid code in 50,000 places (and possibly fat-finger it once).

Re:Up to 12M websites may have been compromised via Drupal v... by opendais on Monday November 03, 2014 @10:47AM (#8551346)
It belongs in an ORM, not the language.
Re:Up to 12M websites may have been compromised via Drupal v... by jiggy2011 on Saturday November 01, 2014 @02:33PM (#8544072)
I don't think that would work in this case, because there is no way to bind a PHP array to an SQL tuple. Available param types includes string , int , bool but no array or tuple type.

http://php.net/manual/en/pdo.constants.php

You can write "SELECT calories FROM fruit WHERE name IN (? , ?, ?)" and then bind the parameters as strings but this will only work in cases where the length of the tuple is known and fixed. If you need to allow for a variable length tuple then you will need to concatenate the query string yourself.

Re:Up to 12M websites may have been compromised via Drupal v... by SapphireSun on Saturday November 01, 2014 @04:29PM (#8544493)
Just use a placeholder like (??) and flatten everything out. Translate (??) and array(1,2,3) into (?,?,?) with 1, 2, 3 as the binds.
Re:Up to 12M websites may have been compromised via Drupal v... by mappu on Sunday November 02, 2014 @12:56AM (#8545670)
Then what do you do if the passed tuple has zero length?

Dynamically rewriting the SQL isn't going to be tractable in all cases - doing `IN (NULL)` might be a valid value - and throwing an exception is poor form for an actually valid case.

Re:Up to 12M websites may have been compromised via Drupal v... by SapphireSun on Wednesday November 05, 2014 @12:45AM (#8560661)
Check for bad inputs? I'm not saying this is a cure all, but it's a lot better than not being able to use arrays at all.
Re:Up to 12M websites may have been compromised via Drupal v... by jiggy2011 on Saturday November 01, 2014 @04:49PM (#8544569)
I don't think PDO supports this AFAIK.
Re:Up to 12M websites may have been compromised via Drupal v... by SapphireSun on Wednesday November 05, 2014 @12:42AM (#8560656)
I had to implement it manually, but it works. FWIW, I'm using a custom wrapper around MySQLi though.
Re:Up to 12M websites may have been compromised via Drupal v... by opendais on Monday November 03, 2014 @10:50AM (#8551357)
You count the length of the array. You create the tuple with the appropriate number of ?'s. You then bind parameters.

No user input involved.

Re:Up to 12M websites may have been compromised via Drupal v... by opendais on Saturday November 01, 2014 @04:37PM (#8544525)
...

What part of you needed to write a function was unclear? o.O

Counting an array is a safe operation.

Re:Up to 12M websites may have been compromised via Drupal v... by jiggy2011 on Saturday November 01, 2014 @04:55PM (#8544581)
You need to count the array at runtime and convert to a tuple, the only way this can be done using PDO (AFAIK) is by appending the tuple to the string itself because PDO provides no tuple type.