-
Notifications
You must be signed in to change notification settings - Fork 2
Open
Description
hey man, Thanks for creating this! so far its making it easier for me to convert out dated mysql_* functions to use pdo.. I also made some changes that I wanted to share with ya to see what you thought of it..
public function bindValue($name, $value){
$this->pdoStatement->bindValue($name, $value, $this->getPDOConstantType($value));
return $this;
}
/*
* check out the insert, update, or delete functions to see how this is used
*/
public function bindValues($arr){
foreach ($arr as $k){
$this->bindValue(':'.$k[0], $k[1]);
}
return $this;
}
private function getPDOConstantType($var){
if(is_int($var))
return PDO::PARAM_INT;
if(is_bool($var))
return PDO::PARAM_BOOL;
if(is_null($var))
return PDO::PARAM_NULL;
//Default
return PDO::PARAM_STR;
}
/*
* returns the number of rows that were effected with UPDATE or DELETE statements
*/
public function rowsEffected(){
return $this->pdoStatement->rowCount();
}
/*
* added a few extra functions to make some things easier
*
*/
function insert($table, $arr){
foreach ($arr as $key => $val) {
$fields[] = '`'.$key.'`';
$params[] = ':'.$key;
$values[] = array($key, $val);
}
$fields = implode(',', $fields);
$params = implode(',', $params);
$sql = "INSERT INTO `".$table."` (".$fields.") VALUES (".$params.")";
$this->prepare($sql)->bindValues($values)->execute();
return $this->lastId();
}
function update($table, $arr, $where, $limit = 1){
foreach($arr as $key => $val){
$fields[] = '`'.$key.'` = :'.$key;
$values[] = array($key, $val);
}
$i = 0;
foreach($where as $k => $v){
$i++;
// the $i is in there because row wouldnt update with :value already being set above
$whe[] = '`'.$k.'` = :'.$k.$i;
$values[] = array($k.$i, $v);
}
$fields = implode(',', $fields);
$where = isset($whe) ? 'WHERE '.implode(' AND ', $whe) : '';
$sql = "UPDATE `".$table."` SET ".$fields." ".$where." LIMIT ".$limit;
$this->prepare($sql)->bindValues($values)->execute();
return $this->rowsEffected();
}
function delete($table, $arr, $limit=1){
foreach($arr as $key => $val){
$fields[] = '`'.$key.'` = :'.$key;
$values[] = array($key, $val);
}
$fields = implode(' AND ', $fields);
$sql = "DELETE FROM `".$table."` WHERE ".$fields." LIMIT ".$limit;
$this->prepare($sql)->bindValues($values)->execute();
return $this->rowsEffected();
}Metadata
Metadata
Assignees
Labels
No labels