diff --git a/src/WPDO.php b/src/WPDO.php index 9a0b1a1..0eee0d9 100644 --- a/src/WPDO.php +++ b/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)); diff --git a/src/WTable.php b/src/WTable.php index d3f8972..2d6aab9 100644 --- a/src/WTable.php +++ b/src/WTable.php @@ -6,6 +6,7 @@ namespace PhangoApp\WPDO; class WTable { public $table; + public $name; public $fields; public function __construct($table, $fields) { diff --git a/tests/WPDOTest.php b/tests/WPDOTest.php index d2adb26..9d11d26 100644 --- a/tests/WPDOTest.php +++ b/tests/WPDOTest.php @@ -54,6 +54,30 @@ final class WPDOTest extends TestCase { } + /** + * @depends testInsert + */ + + public function testSelect() { + + global $pdo; + + $this->assertEquals('table_test', $pdo->table->name); + + $this->assertTrue($pdo->select(['name'], 'WHERE name=? AND type=?', ['first', 1])); + + $arr_result=$pdo->sth->fetchAll(); + + $this->assertEquals([['name' => 'first', 0 => 'first']], $arr_result); + + $this->assertEquals([['name' => 'first', 0 => 'first']], $pdo->select_to_array(['name'], 'WHERE name=? AND type=?', ['first', 1])); + + $this->assertEquals(['name' => 'first', 0 => 'first'], $pdo->select_a_row(['name'], 'WHERE name=? AND type=?', ['first', 1])); + + $this->assertEquals(1, $pdo->select_count('WHERE name=?', ['first'])); + + } + /** * @depends testCreateTable */