mysqli - Creating a php class for database insert functions -
i trying create class save time on cleaning variables before sending them database prevent sql injections. basic systems working cant seem where/or statement implemented. know how add this?
<?php class database { private $db = ''; private $database = ''; function __construct($settings) { $this->db = new mysqli('127.0.0.1', $settings['mysql_user']['username'], $settings['mysql_user']['password']); $this->database = $settings['mysql_user']['database']; print_r('database loaded!<br/>'); } public function query($method, $database, $rows, $params, $where = array(), $or = array()) { $count = 0; $amount = count($rows); $final_rows = ''; $final_data = ''; $bind_names = array(); $bind_names[0] = ''; $param_types = array( "int" => "i", "string" => "s", "double" => "d", "blob" => "b" ); switch($method) { case 'insert': foreach ($rows $row) { $count = $count + 1; $final_rows .= '`' . $row . '`' . ($count != $amount ? ', ' : ''); $final_data .= '?' . ($count != $amount ? ', ' : ''); } $stmt = $this->db->prepare('insert `' . $this->database . '`.`' . $database . '` (' . $final_rows . ') values (' . $final_data . ')'); ($i = 0; $i < count($params); $i++) { $bind_name = 'bind'.$i; $$bind_name = $params[$i][1]; $bind_names[0] .= $param_types[$params[$i][0]]; $bind_names[] = &$$bind_name; } call_user_func_array( array ($stmt, 'bind_param'), $bind_names); return $stmt->execute(); break; case 'update': foreach ($rows $row) { $count = $count + 1; $final_rows .= '`' . $row . '`' . ($count != $amount ? ', ' : ''); $final_data .= '?' . ($count != $amount ? ', ' : ''); } $stmt = $this->db->prepare('update `' . $this->database . '`.`' . $database . '` set ' . $final_rows . ''); ($i = 0; $i < count($params); $i++) { $bind_name = 'bind'.$i; $$bind_name = $params[$i][1]; $bind_names[0] .= $param_types[$params[$i][0]]; $bind_names[] = &$$bind_name; } call_user_func_array( array ($stmt, 'bind_param'), $bind_names); return $stmt->execute(); break; case 'replace': foreach ($rows $row) { $count = $count + 1; $final_rows .= '`' . $row . '`' . ($count != $amount ? ', ' : ''); $final_data .= '?' . ($count != $amount ? ', ' : ''); } $stmt = $this->db->prepare('replace `' . $this->database . '`.`' . $database . '` (' . $final_rows . ') values (' . $final_data . ')'); ($i = 0; $i < count($params); $i++) { $bind_name = 'bind'.$i; $$bind_name = $params[$i][1]; $bind_names[0] .= $param_types[$params[$i][0]]; $bind_names[] = &$$bind_name; } call_user_func_array( array ($stmt, 'bind_param'), $bind_names); return $stmt->execute(); break; } } } ?>
going make few assumptions, first i'll recommend use orm before whipping own solution. here's list of php libraries (i've linked database sections, includes done stand-alone orms https://github.com/ziadoz/awesome-php#database)
that being said i'm going assume $where , $or arrays both construct , items in $where combined via , and $or combined via or.
because didn't describe kind of output looking i'm assuming $where , $or key/value pairs translates "key=value , key=value , (key=value or key=value)".
disclaimer: example kind of hacky, shortest/simplest way example across.
$wherequery = ''; foreach ($where $key => $value) { $wherequery .= "$key = $value and"; } if ($or !== array()) { $wherequery .= '('; foreach ($or $key => $value) { $wherequery .= "$key = $value or"; } } if ($wherequery !== '') { if (($temp = strlen($wherequery) - strlen('and')) >= 0 && strpos($wherequery, 'and', $temp) !== false) { $wherequery = substr($wherequery, -4); } else { $wherequery = substr($wherequery, -3) . ')'; } $wherequery = "where $wherequery"; } you can stick $wherequery @ end of update or select. if $where , $or empty it'll still work.
you move loops functions , make recursive if $value array create more complex statements.
Comments
Post a Comment