Commit c4fbe584 authored by Alex Ne's avatar Alex Ne

PDO

parent 7615a393
...@@ -148,7 +148,7 @@ class PDO { ...@@ -148,7 +148,7 @@ class PDO {
public function insert($table, $data, $replace) { public function insert($table, $data, $replace) {
$keys = array_keys($data); $keys = array_keys($data);
$pattern = array_map(function ($string) {return ":{$string}";}, $keys); $pattern = array_map(function ($key) {return ":{$key}";}, $keys);
if ($replace) { if ($replace) {
$type = "REPLACE"; $type = "REPLACE";
} else { } else {
...@@ -202,22 +202,47 @@ class PDO { ...@@ -202,22 +202,47 @@ class PDO {
$statement->bindValue(":{$key}", $data[$key], \PDO::PARAM_STR); $statement->bindValue(":{$key}", $data[$key], \PDO::PARAM_STR);
} }
} }
foreach ($whete_obj->get_dataset() as $key => $data_item) { $whete_obj->bind($statement);
if (is_integer($data_item)) {
$statement->bindValue($key, $data_item, \PDO::PARAM_INT);
} else {
$statement->bindValue($key, $data_item, \PDO::PARAM_STR);
}
}
return $statement->execute(); return $statement->execute();
} }
/** /**
* @param $table * @param $table
* @param array $where * @param array $where
* @param $columns * @param $order
* @param null $limit
* @param null $columns
* @return mixed
*/
public function simple($table, $where = [], $order = null, $limit = null, $columns = "*") {
$statement = $this->select_statement($table, $where, $order, 1, $columns);
if ($statement->columnCount() > 0) {
return $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
} else {
return false;
}
}
/**
* @param $table
* @param array $where
* @param $order
* @param null $limit
* @param null $columns
* @return mixed
*/ */
public function select($table, $where = [], $order = null, $limit = null, $columns = "*") { public function select($table, $where = [], $order = null, $limit = null, $columns = "*") {
return $this->select_statement($table, $where, $order, $limit, $columns)->fetchAll(\PDO::FETCH_ASSOC);
}
/**
* @param $table
* @param array $where
* @param $order
* @param null $limit
* @param null $columns
*/
public function select_statement($table, $where = [], $order = null, $limit = null, $columns = "*") {
$SQL = "SELECT {$columns} FROM `{$table}` "; $SQL = "SELECT {$columns} FROM `{$table}` ";
$whete_obj = new PDOWhereConstructor($where); $whete_obj = new PDOWhereConstructor($where);
$SQL .= $whete_obj->get_sql(); $SQL .= $whete_obj->get_sql();
...@@ -232,15 +257,10 @@ class PDO { ...@@ -232,15 +257,10 @@ class PDO {
} }
$statement = $this->prepare($SQL); $statement = $this->prepare($SQL);
foreach ($whete_obj->get_dataset() as $key => $data_item) { $whete_obj->bind($statement);
if (is_integer($data_item)) {
$statement->bindValue($key, $data_item, \PDO::PARAM_INT);
} else {
$statement->bindValue($key, $data_item, \PDO::PARAM_STR);
}
}
$statement->execute(); $statement->execute();
return $statement->fetchAll(\PDO::FETCH_ASSOC); return $statement;
} }
} }
?> ?>
\ No newline at end of file
...@@ -192,5 +192,18 @@ class PDOWhereConstructor { ...@@ -192,5 +192,18 @@ class PDOWhereConstructor {
private function not_column_operator($data) { private function not_column_operator($data) {
if (is_array($data)) {return "NOT IN";} else {return "!=";} if (is_array($data)) {return "NOT IN";} else {return "!=";}
} }
/**
* @param $statement
*/
public function bind(&$statement) {
foreach ($this->data_set as $key => $data_item) {
if (is_integer($data_item)) {
$statement->bindValue($key, $data_item, \PDO::PARAM_INT);
} else {
$statement->bindValue($key, $data_item, \PDO::PARAM_STR);
}
}
}
} }
?> ?>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment