Added WPDI for mysqli directly
This commit is contained in:
parent
a1717228bc
commit
bea8e57a33
2 changed files with 407 additions and 0 deletions
296
src/WPDI.php
Normal file
296
src/WPDI.php
Normal file
|
|
@ -0,0 +1,296 @@
|
|||
<?php
|
||||
|
||||
namespace PhangoApp\WPDO;
|
||||
|
||||
class WPDI {
|
||||
|
||||
static public $host_db='';
|
||||
|
||||
static public $db='';
|
||||
|
||||
static public $login_db='';
|
||||
|
||||
static public $pass_db='';
|
||||
|
||||
//static public $driver='mysql';
|
||||
|
||||
static public $mconn=null;
|
||||
|
||||
public $conn;
|
||||
|
||||
public $sth;
|
||||
|
||||
public $table;
|
||||
|
||||
public $last_query;
|
||||
|
||||
public $txt_error='';
|
||||
|
||||
//static public $conn=false;
|
||||
|
||||
public function __construct($table) {
|
||||
|
||||
$this->table=$table;
|
||||
|
||||
if(WPDI::$mconn!=null) {
|
||||
|
||||
$this->conn=WPDI::$mconn;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function connect() {
|
||||
|
||||
if(WPDI::$host_db=='' || WPDI::$db=='') {
|
||||
|
||||
echo 'Cannot connect to the mysqldb';
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
if(WPDI::$mconn==null) {
|
||||
|
||||
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
|
||||
|
||||
//$this->conn=new \PDO(WPDI::$driver.':host='.WPDI::$host_db.';dbname='.WPDI::$db, WPDI::$login_db, WPDI::$pass_db);
|
||||
$this->conn=new mysqli(WPDI::$host_db, WPDI::$login_db, WPDI::$pass_db, WPDI::$db);
|
||||
|
||||
WPDI::$mconn=$this->conn;
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
$this->conn=WPDI::$mconn;
|
||||
|
||||
}
|
||||
|
||||
if($this->conn) {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function query($query, $values) {
|
||||
|
||||
/*$this->sth=$this->conn->prepare($query);
|
||||
|
||||
//$this->sth->execute([$values]);
|
||||
|
||||
return $this->sth->execute($values);*/
|
||||
|
||||
return $mysqli->execute_query($query, $values);
|
||||
|
||||
}
|
||||
|
||||
public function get_result() {
|
||||
|
||||
return $this->sth->fetchAll();
|
||||
|
||||
}
|
||||
|
||||
public function select($fields, $where_sql, $values) {
|
||||
|
||||
$fields=$this->check_fields($fields);
|
||||
|
||||
$str_fields=implode('`, `', $fields);
|
||||
|
||||
$query='select `'.$str_fields.'` from '.$this->table->name.' '.$where_sql;
|
||||
|
||||
/*$this->sth=$this->conn->prepare($query);
|
||||
|
||||
$result=$this->sth->execute($values);
|
||||
|
||||
//$this->last_query=var_dump( $this->sth->queryString, $values);
|
||||
|
||||
return $result;*/
|
||||
|
||||
return $this->conn->execute_query($query, $value);
|
||||
|
||||
}
|
||||
|
||||
public function select_to_array($fields=[], $where_sql='', $values=[]) {
|
||||
|
||||
if(count($fields)==0) {
|
||||
|
||||
$fields=$this->table->fields;
|
||||
|
||||
}
|
||||
|
||||
|
||||
$result=$this->conn->select($fields, $where_sql, $values);
|
||||
|
||||
if($result) {
|
||||
|
||||
//return $this->sth->fetchAll(\PDO::FETCH_ASSOC);
|
||||
$rows=$result->fetch_all(MYSQLI_ASSOC);
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
$this->txt_error=$this->conn->info();
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function select_a_row($fields, $where_sql='', $values=[]) {
|
||||
|
||||
if(count($fields)==0) {
|
||||
|
||||
$fields=$this->table->fields;
|
||||
|
||||
}
|
||||
|
||||
$result=$this->select($fields, $where_sql.' limit 1', $values);
|
||||
|
||||
if($result) {
|
||||
|
||||
$rows=$result->fetch_all(MYSQLI_ASSOC);
|
||||
|
||||
if(count($rows)>0) {
|
||||
|
||||
return $rows[0];
|
||||
|
||||
}
|
||||
|
||||
$this->txt_error=$this->conn->info();
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
$this->txt_error=$this->conn->info();
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function select_count($where_sql, $values) {
|
||||
|
||||
$result=$this->query('select count(*) from '.$this->table->name.' '.$where_sql, $values);
|
||||
|
||||
if($result) {
|
||||
|
||||
$rows=$result->fetch_all(MYSQLI_ASSOC);
|
||||
|
||||
if(count($rows)>0) {
|
||||
|
||||
return $rows[0]['count(*)'];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function insert($values) {
|
||||
|
||||
$fields=array_keys($values);
|
||||
|
||||
$values=array_values($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);
|
||||
|
||||
$query='insert into '.$this->table->name.' (`'.$final_fields.'`) VALUES ('.implode(',', array_fill(0, count($fields), '?')).')';
|
||||
|
||||
//$this->sth=$this->conn->prepare($query);
|
||||
|
||||
//return $this->sth->execute($values);
|
||||
return $this->query($query, $values);
|
||||
|
||||
}
|
||||
|
||||
public function update($fields, $values_update, $where_sql, $values) {
|
||||
|
||||
$fields=$this->check_fields($fields);
|
||||
|
||||
if(count($fields)!=count($values_update)) {
|
||||
|
||||
throw new \Exception('No valid values for insert in '.$this->table->name);
|
||||
|
||||
}
|
||||
|
||||
$final_fields=[];
|
||||
|
||||
foreach($fields as $field) {
|
||||
|
||||
$final_fields[]='`'.$field.'`=?';
|
||||
|
||||
}
|
||||
|
||||
$str_fields=implode(' AND ', $final_fields);
|
||||
|
||||
$query='update '.$this->table->name.' SET '.$str_fields.' '.$where_sql;
|
||||
//echo $query;
|
||||
$final_values=array_merge($values_update, $values);
|
||||
|
||||
/*$this->sth=$this->conn->prepare($query);
|
||||
|
||||
return $this->sth->execute($final_values);*/
|
||||
return $this->query($query, $values);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function check_fields($fields) {
|
||||
|
||||
if($fields[0]!='*') {
|
||||
|
||||
$final_fields=[];
|
||||
|
||||
foreach($fields as $field) {
|
||||
|
||||
if(in_array($field, $this->table->fields)) {
|
||||
|
||||
$final_fields[]=$field;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
$final_fields=['*'];
|
||||
|
||||
}
|
||||
|
||||
if(count($final_fields)==0) {
|
||||
|
||||
throw new \Exception('No valid fields selected in '.$this->table->name.' Valid fields are '.implode(', ', $this->table->fields).' . Fields passed are: '.implode(', ', $fields));
|
||||
|
||||
}
|
||||
|
||||
return $final_fields;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue