Fixes in wpdo
This commit is contained in:
parent
bea8e57a33
commit
9ea2a28f95
4 changed files with 47 additions and 29 deletions
22
src/WPDI.php
22
src/WPDI.php
|
|
@ -44,7 +44,7 @@ class WPDI {
|
||||||
|
|
||||||
if(WPDI::$host_db=='' || WPDI::$db=='') {
|
if(WPDI::$host_db=='' || WPDI::$db=='') {
|
||||||
|
|
||||||
echo 'Cannot connect to the mysqldb';
|
$this->txt_error='Cannot connect to the mysqldb';
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
|
@ -55,7 +55,7 @@ class WPDI {
|
||||||
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
|
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 \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;
|
WPDI::$mconn=$this->conn;
|
||||||
|
|
||||||
|
|
@ -73,6 +73,8 @@ class WPDI {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
||||||
|
$this->txt_error='Cannot connect to the mysqldb';
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -86,16 +88,16 @@ class WPDI {
|
||||||
//$this->sth->execute([$values]);
|
//$this->sth->execute([$values]);
|
||||||
|
|
||||||
return $this->sth->execute($values);*/
|
return $this->sth->execute($values);*/
|
||||||
|
//echo $this->conn;
|
||||||
return $mysqli->execute_query($query, $values);
|
return $this->conn->execute_query($query, $values);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_result() {
|
/*public function get_result() {
|
||||||
|
|
||||||
return $this->sth->fetchAll();
|
return $this->sth->fetchAll();
|
||||||
|
|
||||||
}
|
}*/
|
||||||
|
|
||||||
public function select($fields, $where_sql, $values) {
|
public function select($fields, $where_sql, $values) {
|
||||||
|
|
||||||
|
|
@ -113,7 +115,7 @@ class WPDI {
|
||||||
|
|
||||||
return $result;*/
|
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) {
|
if($result) {
|
||||||
|
|
||||||
//return $this->sth->fetchAll(\PDO::FETCH_ASSOC);
|
//return $this->sth->fetchAll(\PDO::FETCH_ASSOC);
|
||||||
$rows=$result->fetch_all(MYSQLI_ASSOC);
|
$rows=$result->fetch_all(MYSQLI_ASSOC);
|
||||||
|
|
||||||
|
return $rows;
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
||||||
|
|
@ -254,7 +258,7 @@ class WPDI {
|
||||||
/*$this->sth=$this->conn->prepare($query);
|
/*$this->sth=$this->conn->prepare($query);
|
||||||
|
|
||||||
return $this->sth->execute($final_values);*/
|
return $this->sth->execute($final_values);*/
|
||||||
return $this->query($query, $values);
|
return $this->query($query, $final_values);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -140,7 +140,7 @@ class WPDO {
|
||||||
|
|
||||||
if($this->select($fields, $where_sql.' limit 1', $values)) {
|
if($this->select($fields, $where_sql.' limit 1', $values)) {
|
||||||
|
|
||||||
$rows=$this->sth->fetchAll();
|
$rows=$this->sth->fetchAll(\PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
if(count($rows)>0) {
|
if(count($rows)>0) {
|
||||||
|
|
||||||
|
|
@ -163,7 +163,7 @@ class WPDO {
|
||||||
|
|
||||||
if($this->query('select count(*) from '.$this->table->name.' '.$where_sql, $values)) {
|
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) {
|
if(count($rows)>0) {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ include("libraries/Utils.php");
|
||||||
|
|
||||||
Utils::load_config('config_test', 'settings');
|
Utils::load_config('config_test', 'settings');
|
||||||
|
|
||||||
final class WPDOTest extends TestCase {
|
final class WPDITest extends TestCase {
|
||||||
|
|
||||||
public function testConnect()
|
public function testConnect()
|
||||||
{
|
{
|
||||||
|
|
@ -19,7 +19,7 @@ final class WPDOTest extends TestCase {
|
||||||
|
|
||||||
$table=new WTable('table_test', ['name', 'last_name', 'type']);
|
$table=new WTable('table_test', ['name', 'last_name', 'type']);
|
||||||
|
|
||||||
$pdi=new WPDO($table);
|
$pdi=new WPDI($table);
|
||||||
|
|
||||||
$this->assertTrue($pdi->connect());
|
$this->assertTrue($pdi->connect());
|
||||||
|
|
||||||
|
|
@ -29,12 +29,26 @@ final class WPDOTest extends TestCase {
|
||||||
{
|
{
|
||||||
global $pdi;
|
global $pdi;
|
||||||
|
|
||||||
$sql_table="DROP TABLE IF EXISTS table_test;
|
/*CREATE TABLE MyGuests (
|
||||||
CREATE TABLE table_test (
|
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||||
`name` VARCHAR(255) NOT NULL default '',
|
firstname VARCHAR(30) NOT NULL,
|
||||||
`last_name` VARCHAR(255) NOT NULL default '',
|
lastname VARCHAR(30) NOT NULL,
|
||||||
`type` INT 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, []));
|
$this->assertTrue($pdi->query($sql_table, []));
|
||||||
|
|
||||||
|
|
@ -64,15 +78,15 @@ final class WPDOTest extends TestCase {
|
||||||
|
|
||||||
$this->assertEquals('table_test', $pdi->table->name);
|
$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']));
|
$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->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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -70,9 +70,9 @@ final class WPDOTest extends TestCase {
|
||||||
|
|
||||||
$this->assertEquals([['name' => 'first', 0 => 'first']], $arr_result);
|
$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']));
|
$this->assertEquals(1, $pdo->select_count('WHERE name=?', ['first']));
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue