This is my library class. This does not have any issues but I want to know if I have any mistakes, or ways to improve performance:
variable and call (example of SELECT
):
include(class_dbmanager.php)
$this->DBMANAGER = new Class_DbManager();
$Query['DatabaseName'] = "SELECT user FROM table_user WHERE unsername='" . $username . "';";
$BDResult = $this->DBMANAGER->GetData($Query);
variable and call (example of INSERT/UPDATE
):
$Query['DatabaseName'][] = "INSERT INTO Tbl_Sys_Usuarios(IdTUser, Username, Password, Email) VALUES ('$IdTUser', '$Username', '$Password', '$Email');";
$BDResult = $this->DBMANAGER->InsertData($Query);
The library:
<?php
class Class_DbManager {
//for select
//$Query is array with database index and query string
//$Conf is secundary conection data
public function GetData($Query, $Conf = '') {
try {
$DB_R = [];
$DB = [];
$val = [];
$prefix = '';
$count = 0;
if (USEPREFIX == True) {
$prefix = DB_PRE; //prefix DB
}
reset($Query);
$DB_2USE = key($Query);
//Conecction. i use defined const...
$conn = new PDO("mysql:host=" . DB_HOST . ";dbname=" . $prefix . "" . DBSYS . "", DB_USER, DB_PASS);
//secundary Connection
if (isset($Conf['CONF']['ChangeServ'])) {
if ($Conf['CONF']['ChangeServ'] == true) {
$conn = new PDO("mysql:host=" . $Conf['CONF']['DB_HOST'] . ";dbname=" . $Conf['CONF']['PREFIX2USE'] . "" . $Conf['CONF']['DB2USE'] . "", $Query['CONF']['DB_USER'], $Query['CONF']['DB_PASS']);
}
}
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec("set names utf8");
$conn->exec('USE ' . $DB_2USE);
//execution query.
$DB_R['r'] = $conn->query($Query[$DB_2USE], PDO::FETCH_ASSOC);
$count = $DB_R['r']->rowCount();
$DB_R['c'] = $count;
if ($count == 0) {
$DB_R['r'] = null;
} elseif ($count == 1) {
$DB_R['r'] = $DB_R['r']->fetch(); //Fetch result i f result is 1 if not resturn the result unfetch
}
$conn = null;
return $DB_R;
} catch (PDOException $e) {
echo '<pre>';
echo var_dump($e);
echo '<pre>';
}
}
//for Update and Insert
//$Query is array with database index and query string
//$Conf is secundary conection data
public function UpdateData($Query, $Conf = '') {
try {
$DB_R = [];
$DB = [];
$val = [];
$prefix = '';
$cT = 0;
if (USEPREFIX == True) {
$prefix = DB_PRE;
}
$conn = new PDO("mysql:host=" . DB_HOST . ";dbname=" . $prefix . "" . DBSYS . "", DB_USER, DB_PASS);
if (isset($Conf['CONF']['ChangeServ'])) {
if ($Conf['CONF']['ChangeServ'] == true) {
$conn = new PDO("mysql:host=" . $Conf['CONF']['DB_HOST'] . ";dbname=" . $Conf['CONF']['PREFIX2USE'] . "" . $Conf['CONF']['DB2USE'] . "", $Query['CONF']['DB_USER'], $Query['CONF']['DB_PASS']);
}
}
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->beginTransaction();
$conn->exec("set names utf8");
foreach ($Query as $DB_2USE => $QArr) {
$conn->exec('USE ' . $DB_2USE);
foreach ($QArr as $key => $QString) {
$conn->exec($QString);
$cT++;
}
}
$conn->commit();
$conn = null;
$DB_R['r'] = true;
return $DB_R;
} catch (PDOException $e) {
#rollback al autoincrement
$conn->rollback();
$conn->beginTransaction();
$conn->exec("set names utf8");
foreach ($Query as $DB_2USE => $QArr) {
$conn->exec('USE ' . $DB_2USE);
foreach ($QArr as $key => $QString) {
preg_match('/\binto\b\s*(\w+)/i', $QString, $tables);
$conn->exec("ALTER TABLE " . $tables[1] . " AUTO_INCREMENT=1;");
}
}
$conn->commit();
echo '<pre>';
echo var_dump($e);
echo '<pre>';
}
}
//for Insert
//$Query is array with database index and query string
//$Conf is secundary conection data
public function InsertData($Query, $Conf = '') {
try {
$DB_R = [];
$DB = [];
$val = [];
$prefix = '';
$cT = 0;
if (USEPREFIX == True) {
$prefix = DB_PRE;
}
$conn = new PDO("mysql:host=" . DB_HOST . ";dbname=" . $prefix . "" . DBSYS . "", DB_USER, DB_PASS);
if (isset($Conf['CONF']['ChangeServ'])) {
if ($Conf['CONF']['ChangeServ'] == true) {
$conn = new PDO("mysql:host=" . $Conf['CONF']['DB_HOST'] . ";dbname=" . $Conf['CONF']['PREFIX2USE'] . "" . $Conf['CONF']['DB2USE'] . "", $Query['CONF']['DB_USER'], $Query['CONF']['DB_PASS']);
}
}
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->beginTransaction();
$conn->exec("set names utf8");
foreach ($Query as $DB_2USE => $QArr) {
$conn->exec('USE ' . $DB_2USE);
foreach ($QArr as $key => $QString) {
$conn->exec($QString);
$cT++;
}
}
$conn->commit();
$conn = null;
$DB_R['r'] = true;
return $DB_R;
} catch (PDOException $e) {
#rollback al autoincrement
$conn->rollback();
$conn->beginTransaction();
$conn->exec("set names utf8");
foreach ($Query as $DB_2USE => $QArr) {
$conn->exec('USE ' . $DB_2USE);
foreach ($QArr as $key => $QString) {
preg_match('/\binto\b\s*(\w+)/i', $QString, $tables);
$conn->exec("ALTER TABLE " . $tables[1] . " AUTO_INCREMENT=1;");
}
}
$conn->commit();
echo '<pre>';
echo var_dump($e);
echo '<pre>';
}
}
//for delete
//$Query is array with database index and query string
//$Conf is secundary conection data
public function DeleteData($Query, $Conf = '') {
try {
$DB_R = [];
$DB = [];
$val = [];
$prefix = '';
$cT = 0;
if (USEPREFIX == True) {
$prefix = DB_PRE;
}
$conn = new PDO("mysql:host=" . DB_HOST . ";dbname=" . $prefix . "" . DBSYS . "", DB_USER, DB_PASS);
if (isset($Conf['CONF']['ChangeServ'])) {
if ($Conf['CONF']['ChangeServ'] == true) {
$conn = new PDO("mysql:host=" . $Conf['CONF']['DB_HOST'] . ";dbname=" . $Conf['CONF']['PREFIX2USE'] . "" . $Conf['CONF']['DB2USE'] . "", $Query['CONF']['DB_USER'], $Query['CONF']['DB_PASS']);
}
}
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->beginTransaction();
$conn->exec("set names utf8");
foreach ($Query as $DB_2USE => $QArr) {
$conn->exec('USE ' . $DB_2USE);
foreach ($QArr as $key => $QString) {
$conn->exec($QString);
$cT++;
}
}
$conn->commit();
$conn = null;
$DB_R['r'] = true;
return $DB_R;
} catch (PDOException $e) {
#rollback al autoincrement
$conn->rollback();
$conn->beginTransaction();
$conn->exec("set names utf8");
foreach ($Query as $DB_2USE => $QArr) {
$conn->exec('USE ' . $DB_2USE);
foreach ($QArr as $key => $QString) {
preg_match('/\binto\b\s*(\w+)/i', $QString, $tables);
$conn->exec("ALTER TABLE " . $tables[1] . " AUTO_INCREMENT=1;");
}
}
$conn->commit();
echo '<pre>';
echo var_dump($e);
echo '<pre>';
}
}
}
i think add the compatibility for prepared statement but have this issues building the query string:
<?php
//perm analisis simulated create array.
$queryArr = [];
$perms = [];
$perms['p-001'] = array(0, 1, 1, 1, 0);
$perms['p-002'] = array(0, 1, 1, 0, 1);
$perms['p-003'] = array(1, 1, 0, 1, 1);
$perms['p-004'] = array(1, 0, 1, 1, 1);
//query String build
foreach ($perms as $index => $arrayperms) {
if ($arrayperms[0] == 1) {
//insert
$query = "INSERT INTO tb_perms_user(Id_Proccess,create,read,update,delete)value('$index'," . $arrayperms[1] . "," . $arrayperms[2] . "," . $arrayperms[3] . "," . $arrayperms[4] . ");";
} else {
//update
$query = "UPDATE tb_perms_user SET create=" . $arrayperms[1] . ",read=" . $arrayperms[2] . ",update=" . $arrayperms[3] . ",delete=" . $arrayperms[1] . " WHERE Id_Proccess='$index';";
}
$queryArr['databasename'][] = $query;
}
echo '<pre>';
echo var_dump($queryArr);
echo '</pre>';
When i run the script like this:
$BDResult = $this->DBMANAGER->InsertData($queryArr);
$Query
in different ways. \$\endgroup\$"ALTER TABLE " . $tables[1] . " AUTO_INCREMENT=1;"
query? it looks extremely suspicious. Looks like you are shooting yourself in the foot here. Did you actually try this code on a real database? Was it able to work further afterwards? \$\endgroup\$