Fixes for complete test
This commit is contained in:
parent
731bdfcf82
commit
43dc5639b3
3 changed files with 111 additions and 19 deletions
105
src/WPDO.php
105
src/WPDO.php
|
|
@ -14,18 +14,28 @@ class WPDO {
|
|||
|
||||
static public $driver='mysql';
|
||||
|
||||
static public $mconn=null;
|
||||
|
||||
public $conn;
|
||||
|
||||
public $sth;
|
||||
|
||||
public $table;
|
||||
|
||||
public $last_query;
|
||||
|
||||
//static public $conn=false;
|
||||
|
||||
public function __construct($table) {
|
||||
|
||||
$this->table=$table;
|
||||
|
||||
if(WPDO::$mconn!=null) {
|
||||
|
||||
$this->conn=WPDO::$mconn;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function connect() {
|
||||
|
|
@ -38,9 +48,29 @@ class WPDO {
|
|||
|
||||
}
|
||||
|
||||
$this->conn=new \PDO(WPDO::$driver.':host='.WPDO::$host_db.';dbname='.WPDO::$db, WPDO::$login_db, WPDO::$pass_db);
|
||||
if(WPDO::$mconn==null) {
|
||||
|
||||
return true;
|
||||
$this->conn=new \PDO(WPDO::$driver.':host='.WPDO::$host_db.';dbname='.WPDO::$db, WPDO::$login_db, WPDO::$pass_db);
|
||||
|
||||
WPDO::$mconn=$this->conn;
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
$this->conn=WPDO::$mconn;
|
||||
|
||||
}
|
||||
|
||||
if($this->conn) {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -58,35 +88,74 @@ class WPDO {
|
|||
|
||||
$fields=$this->check_fields($fields);
|
||||
|
||||
$query='select '.$str_fields.' from '.$this->table->name.' '.$where_sql;
|
||||
$str_fields=implode('`, `', $fields);
|
||||
|
||||
$query='select `'.$str_fields.'` from '.$this->table->name.' '.$where_sql;
|
||||
|
||||
$this->sth=$this->conn->prepare($query);
|
||||
|
||||
return $this->sth->execute($values);
|
||||
$result=$this->sth->execute($values);
|
||||
|
||||
//$this->last_query=var_dump( $this->sth->queryString, $values);
|
||||
|
||||
return $result;
|
||||
|
||||
}
|
||||
|
||||
public function select_to_array($fields, $where_sql, $values) {
|
||||
|
||||
$this->sth=$this->select($fields, $where_sql, $values);
|
||||
if($this->select($fields, $where_sql, $values)) {
|
||||
|
||||
return $this->sth->fetchAll();
|
||||
return $this->sth->fetchAll();
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function select_a_row($fields, $where_sql, $values) {
|
||||
|
||||
$this->sth=$this->select($fields, $where_sql, $values);
|
||||
if($this->select($fields, $where_sql.' limit 1', $values)) {
|
||||
|
||||
$rows=$this->sth->fetchAll();
|
||||
|
||||
if(count($rows)>0) {
|
||||
$rows=$this->sth->fetchAll();
|
||||
|
||||
return $rows[0];
|
||||
if(count($rows)>0) {
|
||||
|
||||
return $rows[0];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
public function select_count($where_sql, $values) {
|
||||
|
||||
if($this->query('select count(*) from '.$this->table->name.' '.$where_sql, $values)) {
|
||||
|
||||
$rows=$this->sth->fetchAll();
|
||||
|
||||
if(count($rows)>0) {
|
||||
|
||||
return $rows[0]['count(*)'];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -100,12 +169,10 @@ class WPDO {
|
|||
|
||||
}
|
||||
|
||||
$final_fields=implode('`', $fields);
|
||||
|
||||
$quest=implode(',', $fields);
|
||||
|
||||
$query='insert into '.$this->table->name.' (`'.$final_fields.'`) VALUES ('.array_fill(0, count($final_fields), '?').')';
|
||||
$final_fields=implode('`, `', $fields);
|
||||
|
||||
$query='insert into '.$this->table->name.' (`'.$final_fields.'`) VALUES ('.implode(',', array_fill(0, count($fields), '?')).')';
|
||||
//echo $query;
|
||||
$this->sth=$this->conn->prepare($query);
|
||||
|
||||
return $this->sth->execute($values);
|
||||
|
|
@ -118,7 +185,7 @@ class WPDO {
|
|||
|
||||
$final_fields=[];
|
||||
|
||||
foreach($field as $fields) {
|
||||
foreach($fields as $field) {
|
||||
|
||||
if(in_array($field, $this->table->fields)) {
|
||||
|
||||
|
|
@ -134,7 +201,7 @@ class WPDO {
|
|||
$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));
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ namespace PhangoApp\WPDO;
|
|||
class WTable {
|
||||
|
||||
public $table;
|
||||
public $name;
|
||||
public $fields;
|
||||
|
||||
public function __construct($table, $fields) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue