Advertisement
Guest User

loldbs

a guest
Oct 21st, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.99 KB | None | 0 0
  1. <?PHP
  2.  
  3.     /*************************************************
  4.     **                                              **
  5.     **  Class database written by Steven A. Gabarr� **
  6.     **                                              **
  7.     *************************************************/
  8.  
  9.     class database
  10.     {
  11.         private $res;
  12.         private $host = "127.0.0.1"; // change to your own default values
  13.         private $user = "root"; // change to your own default values
  14.         private $pass = ""; // change to your own default values
  15.         private $db = "indexer_database"; // change to your own default values
  16.         private $mysqli;
  17.  
  18.  
  19.         // sets user, pass and host and connects
  20.         public function setup($u, $p, $h, $db)
  21.         {
  22.             $this->user = $u;
  23.             $this->pass = $p;
  24.             $this->host = $h;
  25.             $this->db = $db;
  26.             if (isset($this->mysqli))
  27.                 $this->disconnect();
  28.             $this->connect();
  29.         }
  30.  
  31.         // Changes the database in which all queries will be performed
  32.         public function pick_db($db)
  33.         {
  34.             $this->db = $db;
  35.             if (isset($this->mysqli))
  36.                 $this->mysqli->select_db($db);
  37.             else
  38.                 $this->connect();
  39.         }
  40.  
  41.         // destructor disconnects and frees the results holder
  42.         public function __destruct()
  43.         {
  44.             $this->disconnect();
  45.         }
  46.  
  47.         //Closes the connection to the DB
  48.         public function disconnect()
  49.         {
  50.             if (isset($this->mysqli))
  51.                 $this->mysqli->close();
  52.             if (isset($this->res) && gettype($this->res) == "object")
  53.             {
  54.                 $this->res->free();
  55.             }
  56.             unset($this->res);
  57.             unset($this->mysqli);
  58.         }
  59.    
  60.  
  61.         // connects to the DB or disconnects/reconnects if a connection already existed
  62.         public function connect()
  63.         {
  64.             if (isset($this->mysqli))
  65.                 $this->disconnect();
  66.        
  67.             try {
  68.                 if (!$this->mysqli= new mysqli($this->host, $this->user, $this->pass, $this->db))
  69.                     throw new Exception("Cannot Connect to ".$this->host);
  70.             } catch (Exception $e)
  71.             {
  72.                 echo $e->getMessage();
  73.                 exit;
  74.             }
  75.         }
  76.  
  77.         public function send_sql($sql) {
  78.             if (!isset($this->mysqli))
  79.                 $this->connect();
  80.             try {
  81.                 if (isset($this->res) && gettype($this->res) == "object")
  82.                     $this->res->free();
  83.                 if (! $this->res = $this->mysqli->query($sql))
  84.                     throw new Exception("Could not send query");
  85.             } catch (Exception $e)
  86.             {
  87.                 echo $e->getMessage()."<BR>";
  88.                 echo $this->mysqli->error;
  89.                 exit;
  90.             }
  91.             return $this->res;
  92.         }
  93.  
  94.         // Shows the contents of the $res as a table
  95.         public function printout() {
  96.             if (isset($this->res) && (($this->res->num_rows) > 0))
  97.             {
  98.                 $this->res->data_seek(0);
  99.                 $names = $this->res->fetch_fields();
  100.                 $num = count($names);
  101.                 echo "<table border=1>";
  102.                 echo "<tr>";
  103.                 for ($i=0;$i<$num;$i++){
  104.                    echo "<th>";
  105.                    echo $names[$i]->name;
  106.                    echo "</th>";
  107.                 }
  108.                 echo "</tr>";
  109.                 while ($row  =  $this->res->fetch_row()) {
  110.                     echo "<tr>";
  111.                     foreach ($row as $elem) {
  112.                        echo "<td>$elem</td>";
  113.                     }
  114.                     echo "</tr>";
  115.                 }
  116.                 echo "</table>";
  117.                 $this->res->data_seek(0);
  118.             }
  119.             else
  120.                 echo "There is nothing to print!<BR>";
  121.         }
  122.  
  123.         // returns an array with the next row
  124.         public function next_row()
  125.         {
  126.             if (isset($this->res))
  127.                 return $this->res->fetch_assoc();
  128.             echo "You need to make a query first!!!";
  129.             return false;
  130.         }
  131.  
  132.         // returns the last AUTO_INCREMENT data created
  133.         public function insert_id()
  134.         {
  135.             if (isset($this->mysqli))
  136.             {
  137.                 $id = $this->mysqli->insert_id;
  138.                 if ($id == 0)
  139.                     echo "You did not insert an element that cause an auto-increment ID to be created!<BR>";
  140.                 return $id;
  141.             }
  142.             echo "You are not connected to the database!";
  143.             return false;
  144.         }
  145.  
  146.         // Creates a new DB and selects it
  147.         public function new_db($name)
  148.         {
  149.             if (!isset($this->mysqli))
  150.                 $this->connect();
  151.             $query = "CREATE DATABASE IF NOT EXISTS ".$name;
  152.             try {
  153.                 if (!$this->mysqli->query($query))
  154.                     throw new Exception("Cannot create database ".$name);
  155.                 $this->db = $name;
  156.                 $this->mysqli->select_db($name);
  157.             } catch (Exception $e)
  158.             {
  159.                 echo $e->getMessage()."<BR>";
  160.                 echo mysql_error();
  161.                 exit;
  162.             }
  163.         }
  164.     }
  165.  
  166. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement