Added files
This commit is contained in:
commit
bdab68afd3
5 changed files with 230 additions and 0 deletions
165
src/WPDO.php
Normal file
165
src/WPDO.php
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
<?php
|
||||
|
||||
namespace PhangoApp\WPDO;
|
||||
|
||||
|
||||
class WTable {
|
||||
|
||||
public $table;
|
||||
public $fields;
|
||||
|
||||
public function __construct($table, $fields) {
|
||||
|
||||
$this->name=$table;
|
||||
$this->fields=$fields;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class WPDO {
|
||||
|
||||
static public $host_db='';
|
||||
|
||||
static public $db='';
|
||||
|
||||
static public $login_db='';
|
||||
|
||||
static public $pass_db='';
|
||||
|
||||
static public $driver='mysql';
|
||||
|
||||
public $conn;
|
||||
|
||||
public $sth;
|
||||
|
||||
public $table;
|
||||
|
||||
//static public $conn=false;
|
||||
|
||||
public function __construct($table) {
|
||||
|
||||
$this->table=$table;
|
||||
|
||||
}
|
||||
|
||||
public function connect() {
|
||||
|
||||
if(WPDO::$host_db=='' || WPDO::$db=='') {
|
||||
|
||||
echo 'Cannot connect to the mysqldb';
|
||||
|
||||
exit();
|
||||
|
||||
}
|
||||
|
||||
$this->conn=new \PDO(WPDO::$driver.':host='.WPDO::$host_db.';dbname='.WPDO::$db, WPDO::$login_db, WPDO::$pass_db);
|
||||
|
||||
}
|
||||
|
||||
public function query($query, $values) {
|
||||
|
||||
$this->sth=$this->conn->prepare($query);
|
||||
|
||||
$this->sth->execute([$values]);
|
||||
|
||||
return $this->sth->execute($values);
|
||||
|
||||
}
|
||||
|
||||
public function select($fields, $where_sql, $values) {
|
||||
|
||||
$fields=$this->check_fields($fields);
|
||||
|
||||
$query='select '.$str_fields.' from '.$this->table->name.' '.$where_sql;
|
||||
|
||||
$this->sth=$this->conn->prepare($query);
|
||||
|
||||
$this->sth->execute($values);
|
||||
|
||||
return $this->sth->execute($values);
|
||||
|
||||
}
|
||||
|
||||
public function select_to_array($fields, $where_sql, $values) {
|
||||
|
||||
$this->sth=$this->select($fields, $where_sql, $values);
|
||||
|
||||
return $this->sth->fetchAll();
|
||||
|
||||
}
|
||||
|
||||
public function select_a_row($fields, $where_sql, $values) {
|
||||
|
||||
$this->sth=$this->select($fields, $where_sql, $values);
|
||||
|
||||
$rows=$this->sth->fetchAll();
|
||||
|
||||
if(count($rows)>0) {
|
||||
|
||||
return $rows[0];
|
||||
|
||||
}
|
||||
|
||||
return [];
|
||||
|
||||
}
|
||||
|
||||
public function insert($fields, $values) {
|
||||
|
||||
$fields=$this->check_fields($fields);
|
||||
|
||||
if(count($fields)!=count($values)) {
|
||||
|
||||
throw new Exception('No valid values for insert in '.$this->table->name);
|
||||
|
||||
}
|
||||
|
||||
$final_fields=implode('`', $fields);
|
||||
|
||||
$quest=implode(',', $fields);
|
||||
|
||||
$query='insert into '.$this->table->name.' (`'.$final_fields.'`) VALUES ('.array_fill(0, count($final_fields), '?').')';
|
||||
|
||||
$this->sth=$this->conn->prepare($query);
|
||||
|
||||
return $this->sth->execute($values);
|
||||
|
||||
}
|
||||
|
||||
public function check_fields($fields) {
|
||||
|
||||
if($fields[0]!='*') {
|
||||
|
||||
$final_fields=[];
|
||||
|
||||
foreach($field as $fields) {
|
||||
|
||||
if(in_array($field, $this->table->fields)) {
|
||||
|
||||
$final_fields[]=$field;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$str_fields=implode(', ', $final_fields);
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
$str_fields='*';
|
||||
|
||||
}
|
||||
|
||||
if(count($final_fields)==0) {
|
||||
|
||||
throw new Exception('No valid fields selected in '.$this->table->name);
|
||||
|
||||
}
|
||||
|
||||
return $final_fields;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue