Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DB
- {
- protected $db_host = 'localhost';
- protected $db_user = 'root';
- protected $db_pass = '';
- protected $db_name = 'db_loan';
- private $con = false;
- public $result = array();
- function connect()
- {
- if(!$this->con)
- {
- $myconn = @mysql_connect($this->db_host,$this->db_user,$this->db_pass);
- if($myconn)
- {
- $seldb = @mysql_select_db($this->db_name,$myconn);
- if($seldb)
- {
- $this->con = true;
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- else
- {
- return true;
- }
- }
- function setDatabase($name)
- {
- if($this->con)
- {
- if(@mysql_close())
- {
- $this->con = false;
- $this->results = null;
- $this->db_name = $name;
- $this->connect();
- }
- }
- }
- function tableExists($table)
- {
- $tablesInDb = @mysql_query('SHOW TABLES FROM '.$this->db_name.' LIKE "'.$table.'"');
- if($tablesInDb)
- {
- if(mysql_num_rows($tablesInDb)==1)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- function select($table, $rows = '*', $where = null, $group = null, $order = null, $limit = null)
- {
- $q = 'SELECT '.$rows.' FROM '.$table;
- /*if($where != null)
- $q .= ' WHERE '.$where;
- if($order != null)
- $q .= ' ORDER BY '.$order;
- */
- if (!empty($where)) {
- $q .= " WHERE $where";
- }
- if ($group !== null) {
- $q .= " GROUP BY $group";
- }
- if ($order !== null) {
- $q .= " ORDER BY $order";
- }
- if ($limit !== null && is_array($limit)) {
- $start = (int) $limit['start'];
- $end = (int) $limit['end'];
- $q .= " LIMIT $start, $end";
- }
- $query = @mysql_query($q);
- if($query)
- {
- $this->numResults = mysql_num_rows($query);
- for($i = 0; $i < $this->numResults; $i++)
- {
- $r = mysql_fetch_array($query);
- $key = array_keys($r);
- for($x = 0; $x < count($key); $x++)
- {
- // Sanitizes keys so only alphavalues are allowed
- if(!is_int($key[$x]))
- {
- if(mysql_num_rows($query) > 1)
- $this->result[$i][$key[$x]] = $r[$key[$x]];
- else if(mysql_num_rows($query) < 1)
- $this->result = null;
- else
- $this->result[$key[$x]] = $r[$key[$x]];
- }
- }
- }
- return true;
- }
- else
- {
- return false;
- }
- }
- function insert($table,$values,$rows = null)
- {
- if($this->tableExists($table))
- {
- $insert = 'INSERT INTO '.$table;
- if($rows != null)
- {
- $insert .= ' ('.$rows.')';
- }
- for($i = 0; $i < count($values); $i++)
- {
- if(is_string($values[$i]))
- $values[$i] = '"'.$values[$i].'"';
- }
- $values = implode(',',$values);
- $insert .= ' VALUES ('.$values.')';
- $ins = @mysql_query($insert);
- if($ins)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- function delete($table,$where = null)
- {
- if($this->tableExists($table))
- {
- if($where == null)
- {
- $delete = 'DELETE '.$table;
- }
- else
- {
- $delete = 'DELETE FROM '.$table.' WHERE '.$where;
- }
- $del = @mysql_query($delete);
- if($del)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- function update($table,$rows,$where)
- {
- if($this->tableExists($table))
- {
- for($i = 0; $i < count($where); $i++)
- {
- if($i%2 != 0)
- {
- if(is_string($where[$i]))
- {
- if(($i+1) != null)
- $where[$i] = '"'.$where[$i].'" AND ';
- else
- $where[$i] = '"'.$where[$i].'"';
- }
- }
- }
- $where = implode('',$where);
- $update = 'UPDATE '.$table.' SET ';
- $keys = array_keys($rows);
- for($i = 0; $i < count($rows); $i++)
- {
- if(is_string($rows[$keys[$i]]))
- {
- $update .= $keys[$i].'="'.$rows[$keys[$i]].'"';
- }
- else
- {
- $update .= $keys[$i].'='.$rows[$keys[$i]];
- }
- if($i != count($rows)-1)
- {
- $update .= ',';
- }
- }
- $update .= ' WHERE '.$where;
- $query = @mysql_query($update);
- if($query)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- public function getResult()
- {
- return $this->result;
- }
- function auth($form) {
- $username = $form['username'];
- $password = $form['password'];
- $query = mysql_query("SELECT * FROM user WHERE username='$username' and password = md5('$password')");
- $countResult = mysql_num_rows($query);
- if($countResult>0)
- {
- while($row = mysql_fetch_array($query))
- {
- $_SESSION['User_Level'] = $row['userlevel'];
- $_SESSION['id'] = $row['id'];
- $_SESSION['name'] = $row['username'];
- }
- if($_SESSION['User_Level'] == 1)
- {
- echo "<meta http-equiv='refresh' content=0;URL=addmember.php>";
- exit;
- }
- }
- }
- function session() {
- ob_start();
- session_start();
- if(!(isset($_SESSION['name'])))
- header("Location:index.php");
- }
- function logout() {
- session_destroy();
- header("location:index.php");
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement