I have an array which holds multiple ids's, and i need to to update multiple columns. i am using MySQL IN()
for that. the problem is when i use the query below it works perfectly fine.
public function available($value, $propertyId = array()) {
$sth = $this->dbh->prepare('UPDATE properties p SET p开发者_StackOverflow社区.status = :status WHERE p.id IN('.implode(',',$propertyId).')');
$sth->bindParam(':status',$value);
return $sth->execute();
}
as the above query is not using any placeholders for second argument $propertyId
i assume it is wrong way to do so. but when i use either named or unnamed place holder in the query it will update only 1 row. for example the below codes update only one row.
//This will update only one row.
//Using Named Place Holder
public function available($value, $propertyId = array()) {
$sth = $this->dbh->prepare('UPDATE properties p SET p.status = :status WHERE p.id IN(:propertyId)');
$sth->bindParam(':status',$value);
$sth->bindParam(':propertyId', implode(',', $propertyId));
return $sth->execute();
}
Or
//Using Unnamed Place Holder.
public function available($value, $propertyId = array()) {
$sth = $this->dbh->prepare('UPDATE properties p SET p.status = ? WHERE p.id IN(?)');
return $sth->execute(array($value, implode(',', $propertyId)));
}
i tried converting converting the array to a string and assigning it to the variable like below.
public function available($value, $propertyId = array()) {
$id = implode(',', $propertyId);
$sth = $this->dbh->prepare('UPDATE properties p SET p.status = ? WHERE p.id IN(?)');
return $sth->execute(array($value, $id));
}
even that won't work. what is happening? what am i missing?
thank you
You can do something like this, wish it helps.
public function available($value, $propertyId = array()) {
$id_placeholders = implode(',', array_fill(0, count($propertyId), '?'));
$sth = $this->dbh->prepare('UPDATE properties p SET p.status = ? WHERE p.id IN('.$id_placeholders.')');
return $sth->execute(array_merage(array($value), $propertyId));
}
精彩评论