if ($this->error_log) error_log(trim(str_replace("\n", ' ', $sql)));
}
- private function handleError($sth, $sql)
+ private function handleError($sth, $sql, $data=false)
{
if (!$this->error_log) error_log(trim(str_replace("\n", ' ', $sql)));
$arr = $this->db->errorInfo();
if (count($_POST)) $body .= 'POST: ' . var_export($_POST,true)."\n";
if (count($_GET)) $body .= 'GET: ' . var_export($_GET,true)."\n";
if (count($_SESSION)) $body .= 'SESSION: ' . var_export($_SESSION,true)."\n";
+ if ($data) $body .= 'data: ' . var_export($data,true)."\n";
$mail->send($body);
}
$sth = $this->db->prepare($sql);
if ($sth === false) return $this->handleError($sth,$sql);
- foreach ($data as $k => $v)
- $sth->bindValue(':'.$k, $v); // , PDO::PARAM_STR);
+ foreach ($data as $key => $value) {
+ if(is_int($value))
+ $param = PDO::PARAM_INT;
+ elseif(is_bool($value))
+ $param = PDO::PARAM_BOOL;
+ elseif(is_null($value))
+ $param = PDO::PARAM_NULL;
+ elseif(is_string($value))
+ $param = PDO::PARAM_STR;
+ else
+ $param = FALSE;
+
+ $sth->bindValue(':'.$key, $value, $param);
+ }
- $ok = $this->db->execute();
- if ($ok === false) return $this->handleError($sth,$sql);
+ $ok = $sth->execute();
+ if ($ok === false) return $this->handleError($sth,$sql,$data);
if (preg_match('/INSERT\s+INTO\s+(\S+)\s+/i', $sql, $matches))
$this->lastInsertTable = $matches[1];
- return $sth;
+ return $ok;
}
public function insertInto($table, $data)
implode(',', $columns),
implode(',', $values));
return $this->executeBind($sql, $data);
+ }
- /*
- $db = new PDO('sqlsrv:server=SQLSERVERNAME;Database=own_exchange', 'user', 'password');
- $sql = "INSERT INTO dbo.files(file_name, file_source) VALUES(:file_name, :file_source)";
- $stmt = $db->prepare($sql);
- $stmt->bindParam(":file_name", $files->name, PDO::PARAM_STR);
- $stmt->bindParam(":file_source", file_get_contents($files->tempName), PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
- $stmt->execute();
-
-
- $stmt = $dbh->prepare("INSERT INTO tExample (id,value) VALUES (:id,:value)");
- $taValues = array(
- 'id' => '1',
- 'value' => '2'
- ); // array
- PDOBindArray($stmt,$taValues);
- $stmt->execute();
-
+ public function update($table, $data, $condition)
+ {
+ if (!is_array($data)) throw Exception('update called without data array');
+ if (empty($condition)) throw Exception('update called without condition');
+ $set = array();
+ foreach ($data as $k => $v)
+ $set[] = $k.'=:'.$k;
- */
+ $sql = sprintf("UPDATE %s SET %s\n WHERE %s",
+ $table,
+ implode(',', $set),
+ $condition);
+ return $this->executeBind($sql, $data);
}
}