Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class SQL {
- protected $hostname = 'localhost';
- protected $username = 'root';
- protected $password = 'my-cool-password';
- protected $database = 'testdb';
- protected $connection = false;
- protected $selectdb = false;
- protected $query = false;
- public $result = array();
- function __construct() {
- $this->hostname = ($hostname) ? $hostname : $this->hostname;
- $this->username = ($username) ? $username : $this->username;
- $this->password = ($password) ? $password : $this->password;
- $this->database = ($database) ? $database : $this->database;
- }
- function connect() {
- $this->connection = mysql_connect($this->hostname,$this->username,$this->password) or die(mysql_error());
- if ($this->connection) {
- $this->selectdb = mysql_select_db($this->database) or die(mysql_error());
- if ($this->selectdb) {
- mysql_query("SET NAMES utf8") or die(mysql_error());
- return true;
- }
- else { return false; }
- }
- else { return false; }
- }
- function select($table,$columns = NULL) {
- $columns = (!is_array($columns) || $columns == NULL) ? "*" : implode(",",$columns);
- $this->query = 'SELECT '.$columns.' FROM '.$table;
- return $this;
- }
- function order($by = 'id',$sort = 'DESC') {
- $this->query .= ' ORDER BY '.$by.' '.$sort;
- return $this;
- }
- function limit($start,$end = NULL) {
- $this->query .= ' LIMIT '.$start.($end != NULL ? ','.$end : '');
- return $this;
- }
- function do_query($mode = 'mysql_fetch_row') {
- if (!empty($this->query)) {
- echo 'SQL: '.$this->query.'<br />';
- $queries = mysql_query($this->query);
- while ($row = $mode($queries)) {
- array_push($this->result,$row);
- }
- return $this->result;
- }
- }
- }
- $db = new SQL;
- $db->connect();
- $results = $db->select('categories')->order('id','asc')->limit('1','1')->do_query('mysql_fetch_array');
- foreach ($results AS $key => $value) {
- echo $value['id'].' '.$value['name'].'<br />';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement