Advertisement
Guest User

Untitled

a guest
May 26th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.94 KB | None | 0 0
  1. <?php
  2. class SQL {
  3.      protected $hostname = 'localhost';
  4.      protected $username = 'root';
  5.      protected $password = 'my-cool-password';
  6.      protected $database = 'testdb';
  7.      protected $connection = false;
  8.      protected $selectdb = false;
  9.      protected $query = false;
  10.      public $result = array();
  11.      function __construct() {
  12.          $this->hostname = ($hostname) ? $hostname : $this->hostname;
  13.          $this->username = ($username) ? $username : $this->username;
  14.          $this->password = ($password) ? $password : $this->password;
  15.          $this->database = ($database) ? $database : $this->database;
  16.      }
  17.      function connect() {
  18.          $this->connection = mysql_connect($this->hostname,$this->username,$this->password) or die(mysql_error());
  19.          if ($this->connection) {
  20.              $this->selectdb = mysql_select_db($this->database) or die(mysql_error());
  21.              if ($this->selectdb) {
  22.                  mysql_query("SET NAMES utf8") or die(mysql_error());
  23.                  return true;
  24.              }
  25.              else { return false; }
  26.          }
  27.          else { return false; }
  28.      }
  29.      function select($table,$columns = NULL) {
  30.          $columns = (!is_array($columns) || $columns == NULL) ? "*" : implode(",",$columns);
  31.          $this->query = 'SELECT '.$columns.' FROM '.$table;
  32.          return $this;
  33.      }
  34.      function order($by = 'id',$sort = 'DESC') {
  35.          $this->query .= ' ORDER BY '.$by.' '.$sort;
  36.          return $this;
  37.      }
  38.      function limit($start,$end = NULL) {
  39.          $this->query .= ' LIMIT '.$start.($end != NULL ? ','.$end : '');
  40.          return $this;
  41.      }
  42.      function do_query($mode = 'mysql_fetch_row') {
  43.          if (!empty($this->query)) {
  44.              echo 'SQL: '.$this->query.'<br />';
  45.              $queries = mysql_query($this->query);
  46.              while ($row = $mode($queries)) {
  47.                  array_push($this->result,$row);
  48.              }
  49.              return $this->result;
  50.          }
  51.      }
  52. }
  53. $db = new SQL;
  54. $db->connect();
  55. $results = $db->select('categories')->order('id','asc')->limit('1','1')->do_query('mysql_fetch_array');
  56. foreach ($results AS $key => $value) {
  57.      echo $value['id'].' '.$value['name'].'<br />';
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement