Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $dbh=new PDO($dbConnect, $dbUser, $dbPass);
- $parms=array(12, 45, 65, 33);
- $parmcount=count($parms); // = 4
- $inclause=implode(',',array_fill(0,$parmcount,'?')); // = ?,?,?,?
- $sql='SELECT age, name FROM people WHERE id IN (%s)';
- $preparesql=sprintf($sql,$inclause); // = example statement used in the question
- $st=$dbh->prepare($preparesql);
- $st->execute($parms);
- $sql = "... WHERE id IN (?)";
- $values = array(1, 2, 3, 4);
- $result = $dbw -> prepare ($sql, $values) -> execute ();
- function convertToInt(&$value, $key)
- {
- $value = intval($value);
- }
- $ids = array('12', '45', '65', '33');
- array_walk($ids, 'convertToInt');
- $sql = 'SELECT age, name FROM people WHERE id IN (' . implode(', ', $ids) . ')';
- // $sql will contain SELECT age, name FROM people WHERE id IN (12, 45, 65, 33)
- $query = "SELECT id,studentid,book_title,date FROM contracts WHERE studentid IN ($parameters)";
- $statement = $db->prepare($query);
- //build the bind_param function
- call_user_func_array (array($statement, "bind_param"), $idAr);
- //here is what we used to do before making it dynamic
- //statement->bind_param($paramtype,$v1,$v2);
- $statement->execute();
Add Comment
Please, Sign In to add comment