/ Published in: PHP
In first line U need to create a database file. It can be a simple TXT file without any content. And run function install(). After all U can use this small script for creating guest books and others.
Expand |
Embed | Plain Text
<?php class GuestBook { private $base = ''; private $pass = false; private $conn = false; private $connected = false; public function __construct( $database='', $basepass=false ){ throw new Exception("Can not find database file."); $this->base = $database; $this->pass = $basepass; if ($this->conn = sqlite_open($this->base, 0666, $sqliteerror)) $this->connected = true; else throw new Exception('Can not connect to database! Database error: ' . $sqliteerror); } public function install(){ if( $this->connected ) sqlite_query($this->conn, "CREATE TABLE Messages ( Messagesid INTEGER PRIMARY KEY , Name TEXT NOT NULL , Mail TEXT NOT NULL , Mark INTEGER NOT NULL , Message TEXT NOT NULL , IP TEXT )"); else throw new Exception('Not connected to database.'); } public function addNewMark($name='', $mail='', $mark=0, $message='', $ip='0.0.0.0'){ if( !$this->connected ) throw new Exception('Not connected to database.'); sqlite_query($this->conn, "INSERT INTO Messages (Name,Mail,Mark,Message,IP) VALUES ('$name','$mail',$mark,'$message','$ip')"); } public function getMessages( $count=10, $from=false ){ if( !$this->connected ) throw new Exception('Not connected to database.'); //$ext = ($from)?' WHERE MessagesID > '.$from:''; $ext = ($from)?' OFFSET '.$from:''; $q = sqlite_query($this->conn, "SELECT * FROM Messages LIMIT $count".$ext); return sqlite_fetch_all($q); } public function delete( $id ) { if( !$this->connected ) throw new Exception('Not connected to database.'); sqlite_query($this->conn, "DELETE FROM Messages WHERE MessagesID=$id"); } } //$gb = new GuestBook('book.db'); //$gb->install(); //$gb->addNewMark('Alex', '[email protected]', 5, 'Good!', '127.0.0.1'); //print_r( $gb->getMessages(1,1) ); //$gb->delete(1); ?>
You need to login to post a comment.
