diff --git a/src/WPDI.php b/src/WPDI.php index 526b78c..342c3bb 100644 --- a/src/WPDI.php +++ b/src/WPDI.php @@ -44,7 +44,7 @@ class WPDI { if(WPDI::$host_db=='' || WPDI::$db=='') { - echo 'Cannot connect to the mysqldb'; + $this->txt_error='Cannot connect to the mysqldb'; return false; @@ -55,7 +55,7 @@ class WPDI { 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); + $this->conn=new \mysqli(WPDI::$host_db, WPDI::$login_db, WPDI::$pass_db, WPDI::$db); WPDI::$mconn=$this->conn; @@ -73,6 +73,8 @@ class WPDI { } else { + $this->txt_error='Cannot connect to the mysqldb'; + return false; } @@ -86,16 +88,16 @@ class WPDI { //$this->sth->execute([$values]); return $this->sth->execute($values);*/ - - return $mysqli->execute_query($query, $values); + //echo $this->conn; + return $this->conn->execute_query($query, $values); } - public function get_result() { + /*public function get_result() { return $this->sth->fetchAll(); - } + }*/ public function select($fields, $where_sql, $values) { @@ -113,7 +115,7 @@ class WPDI { return $result;*/ - return $this->conn->execute_query($query, $value); + return $this->conn->execute_query($query, $values); } @@ -126,13 +128,15 @@ class WPDI { } - $result=$this->conn->select($fields, $where_sql, $values); + $result=$this->select($fields, $where_sql, $values); if($result) { //return $this->sth->fetchAll(\PDO::FETCH_ASSOC); $rows=$result->fetch_all(MYSQLI_ASSOC); + return $rows; + } else { @@ -254,7 +258,7 @@ class WPDI { /*$this->sth=$this->conn->prepare($query); return $this->sth->execute($final_values);*/ - return $this->query($query, $values); + return $this->query($query, $final_values); } diff --git a/src/WPDO.php b/src/WPDO.php index 3266749..b48dec8 100644 --- a/src/WPDO.php +++ b/src/WPDO.php @@ -140,7 +140,7 @@ class WPDO { if($this->select($fields, $where_sql.' limit 1', $values)) { - $rows=$this->sth->fetchAll(); + $rows=$this->sth->fetchAll(\PDO::FETCH_ASSOC); if(count($rows)>0) { @@ -163,7 +163,7 @@ class WPDO { if($this->query('select count(*) from '.$this->table->name.' '.$where_sql, $values)) { - $rows=$this->sth->fetchAll(); + $rows=$this->sth->fetchAll(\PDO::FETCH_ASSOC); if(count($rows)>0) { diff --git a/tests/WPDITest.php b/tests/WPDITest.php index 2513efe..013d8be 100644 --- a/tests/WPDITest.php +++ b/tests/WPDITest.php @@ -10,7 +10,7 @@ include("libraries/Utils.php"); Utils::load_config('config_test', 'settings'); -final class WPDOTest extends TestCase { +final class WPDITest extends TestCase { public function testConnect() { @@ -19,7 +19,7 @@ final class WPDOTest extends TestCase { $table=new WTable('table_test', ['name', 'last_name', 'type']); - $pdi=new WPDO($table); + $pdi=new WPDI($table); $this->assertTrue($pdi->connect()); @@ -29,12 +29,26 @@ final class WPDOTest extends TestCase { { global $pdi; - $sql_table="DROP TABLE IF EXISTS table_test; - CREATE TABLE table_test ( - `name` VARCHAR(255) NOT NULL default '', - `last_name` VARCHAR(255) NOT NULL default '', - `type` INT NOT NULL - );"; + /*CREATE TABLE MyGuests ( +id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, +firstname VARCHAR(30) NOT NULL, +lastname VARCHAR(30) NOT NULL, +email VARCHAR(50), +reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP +)*/ + + //$sql_table="DROP TABLE IF EXISTS table_test;CREATE TABLE table_test ( id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30) NOT NULL, last_name VARCHAR(30) NOT NULL, type INT NOT NULL )"; + + $sql_table="DROP TABLE IF EXISTS table_test;"; + + $this->assertTrue($pdi->query($sql_table, [])); + + $sql_table="CREATE TABLE table_test ( \n". + "id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,\n". + "name VARCHAR(30) NOT NULL,\n". + "last_name VARCHAR(30) NOT NULL,\n". + "type INT NOT NULL\n". + " )"; $this->assertTrue($pdi->query($sql_table, [])); @@ -64,15 +78,15 @@ final class WPDOTest extends TestCase { $this->assertEquals('table_test', $pdi->table->name); - $this->assertTrue($pdi->select(['name'], 'WHERE name=? AND type=?', ['first', 1])); + $result=$pdi->select(['name'], 'WHERE name=? AND type=?', ['first', 1]); - $arr_result=$pdi->sth->fetchAll(); + $arr_result=$result->fetch_all(MYSQLI_ASSOC); - $this->assertEquals([['name' => 'first', 0 => 'first']], $arr_result); + $this->assertEquals([['name' => 'first']], $arr_result); - $this->assertEquals([['name' => 'first', 0 => 'first']], $pdi->select_to_array(['name'], 'WHERE name=? AND type=?', ['first', 1])); + $this->assertEquals([['name' => 'first']], $pdi->select_to_array(['name'], 'WHERE name=? AND type=?', ['first', 1])); - $this->assertEquals(['name' => 'first', 0 => 'first'], $pdi->select_a_row(['name'], 'WHERE name=? AND type=?', ['first', 1])); + $this->assertEquals(['name' => 'first'], $pdi->select_a_row(['name'], 'WHERE name=? AND type=?', ['first', 1])); $this->assertEquals(1, $pdi->select_count('WHERE name=?', ['first'])); @@ -88,11 +102,11 @@ final class WPDOTest extends TestCase { $this->assertTrue($pdi->update(['name'], ['first_updated'], 'WHERE name=?', ['first'])); - $this->assertTrue($pdi->select(['name'], 'WHERE name=? AND type=?', ['first_updated', 1])); + $result=$pdi->select(['name'], 'WHERE name=? AND type=?', ['first_updated', 1]); - $arr_result=$pdi->sth->fetchAll(); + $arr_result=$result->fetch_all(MYSQLI_ASSOC); - $this->assertEquals([['name' => 'first_updated', 0 => 'first_updated']], $arr_result); + $this->assertEquals([['name' => 'first_updated']], $arr_result); } diff --git a/tests/WPDOTest.php b/tests/WPDOTest.php index 280fe62..9aa92dc 100644 --- a/tests/WPDOTest.php +++ b/tests/WPDOTest.php @@ -70,9 +70,9 @@ final class WPDOTest extends TestCase { $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']], $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(['name' => 'first'], $pdo->select_a_row(['name'], 'WHERE name=? AND type=?', ['first', 1])); $this->assertEquals(1, $pdo->select_count('WHERE name=?', ['first']));