Mange tak for de gode svar, jeg har ændret mine == til === da det simpelthen var en skrivefejl. Og så har jeg også fundet ud af hvordan jeg vil skrive mine strings, om jeg vil bruge 'Blah'.$var eller "blah $var" osv.
Jeg har ændret mine $row osv til $field. I har hjulpet mig rigtig meget med at se de små fejl som jeg har haft efter min pause.
Her er den endelige kode, hvis i har lyst til at se på den:
class.database.php
- <?php
- /*
- **Lets bring out the amazing toolbox!
- */
-
- include_once('class.toolbox.php');
-
-
- class database extends toolbox {
-
- /*
- **Version
- */
- public $version = '0.1.7';
-
- /*
- ** Store single instance of Database
- */
- private static $instance = null;
-
- /*
- **Random $variables
- */
- private $con = false; // Checks to see if the connection is active
- private $order;
- private $limit;
- private $select;
-
- /*
- **Connection handler
- */
- private $mysqli;
-
- /*
- **Connect function
- */
- private function __construct() {
-
- $this->mysqli = new mysqli($this->mysql_host, $this->mysql_user, $this->mysql_pw, $this->mysql_db);
-
- //Check for errors during connection.
- if($this->mysqli->connect_errno) {
-
- $this->log($this->mysqli->connect_error);
- exit('Error connecting to host');
-
- }
-
- return true;
-
- }
-
- /*
- **Singleton getInstance function
- */
- public static function get_instance() {
-
- if(self::$instance===null)
- self::$instance = new self;
-
- return self::$instance;
-
- }
-
- /*
- **Close connection function
- */
- public function close_connection() {
-
- $this->mysqli->close();
-
- }
-
- /*
- **Execute function
- */
- public function execute($query) {
- $this->reset();
- if(!$result = $this->mysqli->query($query)) {
- $this->log($this->mysqli->error.' Query: '.$query);
- return false;
- }
- else
- return $result;
-
- }
-
- /*
- **Delete function
- */
- public function delete($table) {
- $delete = 'DELETE FROM '.$this->sanitize($table);
-
- if(isset($this->where))
- $delete .= ' WHERE '.$this->where;
- if(isset($this->limit))
- $delete .= ' LIMIT '. $this->limit;
-
- if($this->execute($delete)===false)
- return false;
- else
- return $this->mysqli->affected_rows;
-
- }
-
- /*
- **Update function
- */
- public function update($table, $changes) {
-
- $update = 'UPDATE '.$this->sanitize($table).' SET ';
- foreach($changes as $field=>$value) {
- $update .= "`".$this->sanitize($field)."`='".$this->sanitize($value)."',";
- }
-
- //Remove our last comma
- $update = substr($update, 0, -1);
-
- if(isset($this->where))
- $update.= 'WHERE '. $this->where;
-
- if($this->execute($update)===false)
- return false;
- else
- return $this->mysqli->affected_rows;
-
-
- }
-
- /*
- **Insert Function
- */
- public function insert($table, $data) {
-
- $fields = '';
- $values = '';
-
- foreach($data as $f=>$v) {
-
- $fields .= $this->sanitize("`{$f}`,");
- $values .= (is_numeric($v) && (intval($v)==$v) ? $this->sanitize($v)."," : "'".$this->sanitize($v)."',");
-
- }
-
- //Remove commas
- $fields = substr($fields, 0, -1);
- $values = substr($values, 0, -1);
-
- $insert = 'INSERT INTO '.$this->sanitize($table).' ('.$fields.') VALUES('.$values.')';
- if($this->execute($insert)===false)
- return false;
- else
- return $this->mysqli->affected_rows;
-
- }
-
- /*
- **Get function
- */
- public function get($table) {
-
- if(isset($this->select))
- $get = 'SELECT '.$this->select;
- else
- $get = "SELECT *";
-
- $get .= ' FROM '.$this->sanitize($table);
-
- if(isset($this->where))
- $get .= ' WHERE '.$this->where;
- if(isset($this->order))
- $get .= ' ORDER BY '.$this->order;
- if(isset($this->limit))
- $get .= ' LIMIT '.$this->limit;
-
- $result = $this->execute($get);
- if($result===false)
- return false;
-
- if($result->num_rows > 0) {
-
- while($field = $result->fetch_object())
- $x[] = $field;
-
- return $x;
-
- }
- else
- return $result->num_rows;
-
- }
-
- /*
- **Order function
- */
- public function order($field, $how) {
- $this->order = $this->sanitize('`'.$field.'`'.$how);
- }
-
- /*
- **Limit function
- */
- public function limit($first, $second = 0) {
- $limit = $this->sanitize($first);
- if($second!=0)
- $limit .= ','.$this->sanitize($second);
-
- $this->limit = $limit;
-
- }
-
- /*
- **Select function
- */
- public function select($data) {
- $this->select = $this->sanitize($data);
- }
-
- /*
- **Where function
- */
- public function where($field, $value) {
- $field = $this->sanitize($field);
- $value = $this->sanitize($value);
- if(isset($this->where))
- $where .= ' AND '.$field.' = \''.$value.'\'';
- else
- $where = $field.' = \''.$value.'\'';
- }
-
- /*
- **Where or function
- */
- public function where_or($field, $value, $field2, $value2) {
- $field = $this->sanitize($field);
- $value = $this->sanitize($value);
- $field2 = $this->sanitize($field2);
- $value2 = $this->sanitize($value2);
- if(isset($this->where))
- $this->where .= ' AND '.$field.' = \''.$value.'\' OR '.$field2.' = \''.$value2.'\'';
- else
- $this->where = $field.' = \''.$value.'\' OR '.$field2.' = \''.$value2.'\'';
- }
-
- /*
- **Sanitize function
- */
- public function sanitize($str) {
- return $this->mysqli->real_escape_string($str);
- }
-
- /*
- **Reset function
- */
- public function reset() {
- unset($this->order);
- unset($this->limit);
- unset($this->select);
- unset($this->where);
-
- return true;
- }
-
- /*
- **__destruct function
- */
- public function __destruct() {
- $this->close_connection();
- }
-
-
- }
-
- ?>
class.toolbox.php
- <?php
-
- class toolbox {
-
- /*
- **Version
- */
- public $version = '0.1.6';
-
- /*
- **Mysql login credentials
- */
- public $mysql_host = 'localhost';
- public $mysql_user = 'root';
- public $mysql_pw = 'root';
- public $mysql_db = 'toolbox';
-
- public $domain = 'http://localhost:8888/'; /* Domain name here */
- public $document_root = '/helpers';
-
- /*
- **Database function
- */
- public function database() {
- return database::get_instance();
- }
- /*
- **Domain function, if anybody likes it this way?!
- */
- public function get_domain() {
- return $this->domain;
- }
-
- /*
- **Get root function, if anybody likes it this way?!
- */
- public function get_root() {
- return $this->document_root;
- }
-
- /*
- ** Remove spaces from a string
- */
- public function remove_space($var) {
- return str_replace(' ', '', $var);
- }
-
- /*
- **Get extension from a file, careful though! only works if the file doesn't have "." in it's name
- */
- public function extension($var) {
- return strrchr($var, '.');
- }
-
- /*
- **Create a unique string based on time
- */
- public function unique_string() {
- return substr(md5(time()), 0, 5);
- }
-
- /*
- **Create a html redirect
- */
- public function redirect($url, $seconds, $message) {
- $url = $this->domain.$url;
- $data = '<meta HTTP-EQUIV="REFRESH" content="'.$seconds.'; url='.$url.'">';
- $data .= $message;
-
- return $data;
- }
-
-
- /*
- **Anchor function
- */
- public function anchor($link, $text, $other = '') {
-
- if($other==='outside')
- $link = $link;
- else
- $link = $this->domain . $link;
-
- $data = '<a href="'. $link .'"';
- $data .= ' '. $other;
- $data .= '>';
- $data .= $text;
- $data .= '</a>';
-
- return $data;
-
- }
- /*
- **Break function
- */
- public function br($breaks = 1) {
- $data = '';
- for($i=1;$i<=$breaks;$i++)
- $data .= '<br />';
-
- return $data;
- }
-
- /*
- **New line function
- */
- public function nl($nls = 1) {
-
- $data = '';
- for($i=1;$i<=$nls;$i++)
- $data .= "\n";
-
- return $data;
-
- }
-
- /*
- **New line and break function combined
- */
- public function nlbr($lines = 1) {
-
- $data = '';
- for($i=1;$i<=$lines;$i++)
- $data .= '<br />'."\n";
-
- return $data;
-
- }
-
- /*
- **Heading function
- */
- public function heading($heading, $version, $extra = '') {
-
- if(!is_int($version)) $version = 2;
-
- $data = '<h'. $version;
- $data .= ' '. $extra;
- $data .= '>';
- $data .= $heading;
- $data .= '</h'.$version.'>';
-
- return $data;
- }
-
- /*
- **Php self function
- */
- public function phpSelf() {
- return $_SERVER['PHP_SELF'];
- }
-
- /*
- **Post functions
- */
- public function post($index) {
- if(isset($_POST[$index]))
- return $_POST[$index];
- else
- return false;
-
- }
-
- /*
- **Segment function
- */
- public function segment($number, $dummies='') {
-
- $params = explode('/', substr($_SERVER['PHP_SELF'], 1));
-
- if($number=='all')
- return $params;
- else
- if($dummies==true) {
- return $params[($number-1)];
- }
- else
- return $params[$number];
-
- }
-
- /*
- **Detect language of browser function
- */
- public function detect_language() {
-
- $language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
-
- if($language=='da')
- $language = 'danish';
- elseif($language=='en')
- $language = 'english';
-
- return $language;
-
- }
-
- /*
- **Test language function, to add your own language to detect_language() function above ^^
- */
- public function test_language() {
- return $language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
- }
-
- /*
- **Log function
- */
- public function log($msg) {
-
- $date = date('H:i:s d-m-Y');
- $date2 = date('d-m-Y');
- /*
- **You may need to edit filename to the right directory
- */
- $filename = '../logs/'.$date2.'.log';
-
- $log_content = $date." ";
- $log_content .= $msg." ";
- $log_content .= $_SERVER['REMOTE_ADDR']." ";
- $log_content .= $_SERVER['PHP_SELF'];
- $log_content .= "\n";
-
- if(file_exists($filename)) {
- $log = file_get_contents($filename);
- $log .= $log_content;
- file_put_contents($filename, $log);
- }
- else
- file_put_contents($filename, $log_content);
-
- }
-
- }
-
-
-
- ?>
Og en usage.php, som viser hvordan jeg bruger database klassen:
- <?php
- function __autoload($class_name) {
- include_once('class.'.$class_name.'.php');
- }
-
- $tb = new toolbox;
- $db = $tb->database();
-
- /*
- **Delete
- */
- $db->where('username', 'haha_insert');
- $db->limit(1);
- if($db->delete('users'))
- echo 'Delete successful';
-
- echo $tb->nlbr();
-
- /*
- **Update
- */
- $data = array(
- 'username' => 'haha_update',
- 'password' => 'haha_update2'
- );
-
- $db->where('username', 'haha_update');
- $update = $db->update('users', $data);
- if($update)
- echo 'Update Successful';
-
- echo $tb->nlbr();;
-
- /*
- **Insert
- */
- $data = array(
- 'username'=>'haha_insert',
- 'password'=>'haha_insert'
- );
-
-
- $insert = $db->insert('users', $data);
- if($insert)
- echo 'Insert Successful';
-
- echo $tb->nlbr(2);
-
- /*
- **Insert
- */
- $data = array(
- 'username'=>'haha_update',
- 'password'=>'haha_update'
- );
-
-
- $insert = $db->insert('users', $data);
-
-
- /*
- **Get
- **Also works with $db->where() function
- */
- $db->order('userid', 'DESC');
- $db->limit(3);
- $db->where_or('username', 'haha_update', 'username', 'haha_insert');
- $db->select('username, password');
- $get = $db->get('users');
- if($get===false)
- echo $get;
- elseif($get===0)
- echo 'Didn\'t find anything :(';
- else {
- foreach($get as $r) {
- //echo $r->userid.$tb->nlbr();
- echo $r->username.$tb->nlbr();
- echo $r->password.$tb->nlbr(2);
- }
- }
-
- echo $tb->br(4).$tb->nl(4);
-
- $db->order('userid', 'DESC');
- $get = $db->get('users');
- foreach($get as $r) {
- echo $r->userid.$tb->nlbr();
- echo $r->username.$tb->nlbr();
- echo $r->password.$tb->nlbr(2);
- }
-
- ?>