Skip to content

Commit 9e58b52

Browse files
authored
Merge pull request #23 from bim-g/version_boss
[FIX] Insert return `-1` as lastID
2 parents 2741bdb + e883684 commit 9e58b52

6 files changed

Lines changed: 34 additions & 29 deletions

File tree

src/DB.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88
namespace Wepesi\App;
99
use Exception;
10-
use PDO;
1110
use Wepesi\App\Traits\BuildQuery;
1211

1312
class DB
@@ -18,13 +17,13 @@ class DB
1817
private ?string $_error;
1918
private array $_results;
2019
private int $_lastID;
21-
private PDO $pdo;
20+
private \PDO $pdo;
2221
private string $_action="";
2322
private int $_count;
2423
private array $option=[
25-
PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES utf8",
26-
PDO::ATTR_EMULATE_PREPARES=>false,
27-
PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION
24+
\PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES utf8",
25+
\PDO::ATTR_EMULATE_PREPARES=>false,
26+
\PDO::ATTR_ERRMODE=>\PDO::ERRMODE_EXCEPTION
2827
] ;
2928
use BuildQuery;
3029
private function __construct(string $host="",string $db_name="",string $user_name="",string $password="")
@@ -35,7 +34,7 @@ private function __construct(string $host="",string $db_name="",string $user_nam
3534
$this->_lastID = -1;
3635
$this->_count = 0;
3736
//
38-
$this->pdo = new PDO("mysql:host=" . $host . ";dbname=" . $db_name.";charset=utf8mb4", $user_name,$password,$this->option);
37+
$this->pdo = new \PDO("mysql:host=" . $host . ";dbname=" . $db_name.";charset=utf8mb4", $user_name,$password,$this->option);
3938
} catch (\PDOException $ex) {
4039
echo $ex->getMessage();
4140
die();
@@ -143,7 +142,7 @@ function query($sql, array $params = []): DB
143142
$this->_results = $q['result'];
144143
$this->_count = $q['count'];
145144
$this->_error = $q['error'];
146-
$this->_lastID = $q['lastID'];
145+
$this->_lastID = $q['lastID']??-1;
147146

148147
return $this;
149148
}
@@ -154,7 +153,7 @@ function query($sql, array $params = []): DB
154153
function lastId(): int
155154
{
156155
if (isset($this->query_transaction) && method_exists($this->query_transaction, 'lastId')) {
157-
$this->_count = $this->query_transaction->lastId();
156+
$this->_lastID = $this->query_transaction->lastId();
158157
}
159158
return $this->_lastID;
160159
}

src/DB_Insert.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
*/
88
namespace Wepesi\App;
99

10-
class DB_Insert
10+
use Wepesi\App\Traits\BuildQuery;
11+
12+
class DB_Insert
1113
{
1214
private string $table;
1315
private \PDO $pdo;
@@ -69,8 +71,8 @@ private function query(string $sql, array $params = [])
6971
{
7072
$q = $this->executeQuery($this->pdo, $sql, $params);
7173
$this->_results = $q['result'];
72-
$this->_error = $q['error'];
7374
$this->lastID = $q['lastID']??0;
75+
$this->_error = $q['error'];
7476
}
7577

7678
/**

src/DB_Select.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class DB_Select
1414
private ?string $table, $action,$_error,$_dsc,$_asc,$orderBy,$groupBY;
1515
private array $_where, $_fields;
1616
private ?int $_limit, $_offset;
17-
private array $_join_comparison_sign=["=",">","<","!=","<>"];
17+
private array $_join_comparison_sign;
1818
use BuildQuery;
1919

2020
/**
@@ -30,18 +30,18 @@ function __construct(\PDO $pdo, string $table, string $action = null)
3030
$this->action = $action;
3131
$this->_dsc = $this->_asc=null;
3232
$this->_where = $this->_fields =[];
33-
$this->_error =null;
34-
$this->orderBy = $this->groupBY =null;
35-
$this->_limit = $this->_offset=null;
33+
$this->_error = null;
34+
$this->orderBy = $this->groupBY = null;
35+
$this->_limit = $this->_offset = null;
36+
$this->_join_comparison_sign = ['=', '>', '<', '!=', '<>'];
3637
}
3738

3839
/**
39-
*
4040
* @param array $params
4141
* @return $this
42-
* @throws Exception
42+
* @throws \Exception
4343
*/
44-
function where(array $params = [])
44+
function where(array $params = []): DB_Select
4545
{
4646
if (count($params)) {
4747
// $params = [];

src/Traits/BuildQuery.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ protected function executeQuery(\PDO $pdo, string $sql, array $params = []): arr
1616
'result' => [],
1717
'lastID' => -1,
1818
'count' => null,
19-
'error' => null,
19+
'error' => "",
2020
];
2121
$query = $pdo->prepare($sql);
2222
$x = 1;
@@ -26,15 +26,18 @@ protected function executeQuery(\PDO $pdo, string $sql, array $params = []): arr
2626
$x++;
2727
}
2828
}
29-
$query->execute();
29+
$query_result = $query->execute();
3030

31-
if (strchr(strtolower($sql), 'update') || strchr(strtolower($sql), 'select')) {
32-
$data_result['result'] = $query->fetchAll(\PDO::FETCH_OBJ);
33-
$data_result['count'] = $query->rowCount();
34-
} else if (strchr(strtolower($sql), 'insert into')) {
35-
$data_result['lastID'] = $pdo->lastInsertId();
36-
} else if (strchr(strtolower($sql), 'delete')) {
37-
$data_result['result'] = ['delete' => true];
31+
if($query_result){
32+
$data_result['result'] = ['query_result' => true];
33+
if (strchr(strtolower($sql), 'update') || strchr(strtolower($sql), 'select')) {
34+
$data_result['result'] = $query->fetchAll(\PDO::FETCH_OBJ);
35+
$data_result['count'] = $query->rowCount();
36+
} else if (strchr(strtolower($sql), 'insert into')) {
37+
$data_result['lastID'] = $pdo->lastInsertId();
38+
} else if (strchr(strtolower($sql), 'delete')) {
39+
$data_result['result'] = ['delete' => true];
40+
}
3841
}
3942
return $data_result;
4043
} catch (\PDOException $ex) {

test/index.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"password"=>""
99
];
1010
$db = DB::getInstance($config);
11-
// include("./test/insert.php");
12-
include("./test/select.php");
11+
include("./test/insert.php");
12+
//include("./test/select.php");
1313
// include("./test/delete.php");
1414
// include("./test/query.php");
1515
// include("./test/update.php");

test/insert.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
if($db->error()){
1111
throw new \Exception($db->error());
1212
}
13-
var_dump(["last insert ID : "=>$db->lastId()]);
13+
$field["id"] = $db->lastId();
14+
var_dump($field);
1415
} catch (Exception $e) {
1516
var_dump($e->getMessage());
1617
}

0 commit comments

Comments
 (0)