This Mod is for people who still use mysqli instead of PDO.
You can use prepared statements to avoid sql injection.
replace the code in mysql.class.php
replace the mysqli connection code inside function dbconn() in functions.php
USAGE
SQL_Query_exec(Query); - normal query execution
SQL_Query_exec(Query,Parameters); - prepared statement
default identifier is string (s) if you want to mention your identifier
SQL_Query_exec(Query,Parameters,identifier);
for return
SQL_Query_exec(Query,Parameters,identifier,return_type);
default is get_result();
for '1' - will return insert_id; (return auto increment id of the table).
for 'i' = will return 1, if query was successfully executed
if i want to know whether query was executed or not
this will return 1 if query executed.
You can use prepared statements to avoid sql injection.
replace the code in mysql.class.php
<?php
$db_debug = 'false';
function &SQL_Query ($query) {
return new SQL_Query($query);
}
function &SQL_Query_exec ($query,$bind = '',$iden = '',$r = '0') {
$sql = new SQL_Query($query,$bind,$iden,$r);
return $sql->execute();
}
function get_result($q){
$ret = [];
while($r = $q->fetch_assoc()){
$ret[] = $r;
}
unset($r);
mysqli_free_result($q);
return $ret;
}
class SQL_Query {
private $query = "";
private $params = [];
private $bind;
private $iden;
private $rt;
function &__construct ($query,$bind,$iden,$rt) {
$this->query = $query;
$this->bind = $bind;
$this->iden = $iden;
$this->rtype = $rt;
return $this;
}
function &p ($param) {
if (is_numeric($param)) {
$this->params[] = $param;
} elseif (is_array($param)) {
$this->params[] = implode(", ", array_map(array(&$this, "escape"), $param));
} else {
$this->params[] = $this->escape($param);
}
return $this;
}
function &p_name ($param) {
$this->params[] = "`".$GLOBALS["DBconnector"]->real_escape_string($param)."`";
return $this;
}
function escape ($s) {
if(is_numeric($s)){
return $s;
}else{
return "'".$GLOBALS["DBconnector"]->real_escape_string($s)."'";
}
}
// thanks to micro
function read () {
$ret = "";
if (count($this->params)) {
reset($this->params);
for ($i = 0; $i < strlen($this->query); $i++) {
if ($this->query[$i] == "?") {
$val = current($this->params); //new var to replace thisEach
next($this->params); //new var to replace thisEach
$ret .= $val;
} else {
$ret .= $this->query[$i];
}
}
reset($this->params);
} else {
$ret = $this->query;
}
return $ret;
}
function &execute() {
global $db_debug;
$par = $this->bind; //in array
if(empty($par)){
$query = $this->read();
$res = $GLOBALS["DBconnector"]->query($query);
}else{
$query = $this->query;
$iden = $this->iden;
$stmt = $GLOBALS["DBconnector"]->prepare($query);
if(empty($iden)){
$iden = str_repeat("s",count($par));
}else{
}
$stmt->bind_param($iden, ...$par);
$stmt->execute();
if(($this->rtype)=='1'){
$res = $GLOBALS["DBconnector"]->insert_id;
}elseif(($this->rtype)=='i'){
$res = '1';
}else{
$res = $stmt->get_result();
}
}
$mysqli_error = $GLOBALS["DBconnector"]->error;
$mysqli_errno = $GLOBALS["DBconnector"]->errno;
if ($res || $mysqli_errno == 1062) {
return $res;
}
// If debug_backtrace() is available, we can find exactly where the query was called from
if (function_exists("debug_backtrace")) {
$bt = debug_backtrace();
$i = 1;
if ($bt[$i]["function"] == "SQL_Query_exec_cached" || $bt[$i]["function"] == "get_row_count_cached" || $bt[$i]["function"] == "get_row_count")
$i++;
$line = $bt[$i]["line"];
$file = str_replace(getcwd().DIRECTORY_SEPARATOR, "", $bt[$i]["file"]);
$msg = "Database Error in $file on line $line: $mysqli_error. Query was: $query.";
} else {
$file = str_replace(getcwd().DIRECTORY_SEPARATOR, "", $_SERVER["SCRIPT_FILENAME"]);
$msg = "Database Error in $file: $mysqli_error. Query was: $query";
}
$GLOBALS["DBconnector"]->query("INSERT INTO `sqlerr` (`txt`, `time`) VALUES (".sqlesc($msg).", '".get_date_time()."')");
if($db_debug=="true"){
if ( function_exists('show_error_msg') ){
show_error_msg("Database Error", "Database Error. Please report this to an administrator.", 1);
}
}
}
}
?>
replace the mysqli connection code inside function dbconn() in functions.php
function_exists("mysqli_connect") or die("MySQLi support not available.");
$GLOBALS["DBconnector"] = new mysqli($mysql_host, $mysql_user, $mysql_pass,$mysql_db);
// Check connection
if ($GLOBALS["DBconnector"]->connect_error) {
die("Connection failed: " . $GLOBALS["DBconnector"]->connect_error);
}
unset($mysql_pass); //security
USAGE
SQL_Query_exec(Query); - normal query execution
SQL_Query_exec(Query,Parameters); - prepared statement
SQL_Query_exec("SELECT username FROM users WHERE id=?",['1']);
default identifier is string (s) if you want to mention your identifier
SQL_Query_exec(Query,Parameters,identifier);
SQL_Query_exec("SELECT username FROM users WHERE id=?",['1'],"i");
for return
SQL_Query_exec(Query,Parameters,identifier,return_type);
default is get_result();
for '1' - will return insert_id; (return auto increment id of the table).
for 'i' = will return 1, if query was successfully executed
if i want to know whether query was executed or not
SQL_Query_exec("INSERT INTO users (username) VALUES(?)",['admin'],"s","i");
this will return 1 if query executed.
follow the thread for updates
checkout my project: https://bvlist.com/showthread.php?t=12349
checkout my project: https://bvlist.com/showthread.php?t=12349
Last edited by JohnHasher on 2021-12-18 07:49:31