Posted By

AlexSnet on 04/17/10


Tagged

php book SQLite guest


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

joacoleo


Guest Book


 / 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.

  1. <?php
  2. class GuestBook {
  3.  
  4. private $base = '';
  5. private $pass = false;
  6. private $conn = false;
  7.  
  8. private $connected = false;
  9.  
  10. public function __construct( $database='', $basepass=false ){
  11. if( !file_exists($database) )
  12. throw new Exception("Can not find database file.");
  13. $this->base = $database;
  14. $this->pass = $basepass;
  15. if ($this->conn = sqlite_open($this->base, 0666, $sqliteerror))
  16. $this->connected = true;
  17. else
  18. throw new Exception('Can not connect to database! Database error: ' . $sqliteerror);
  19. }
  20.  
  21. public function install(){
  22. if( $this->connected )
  23. 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 )");
  24. else
  25. throw new Exception('Not connected to database.');
  26. }
  27.  
  28. public function addNewMark($name='', $mail='', $mark=0, $message='', $ip='0.0.0.0'){
  29. if( !$this->connected )
  30. throw new Exception('Not connected to database.');
  31. sqlite_query($this->conn, "INSERT INTO Messages (Name,Mail,Mark,Message,IP) VALUES ('$name','$mail',$mark,'$message','$ip')");
  32. }
  33.  
  34. public function getMessages( $count=10, $from=false ){
  35. if( !$this->connected )
  36. throw new Exception('Not connected to database.');
  37. //$ext = ($from)?' WHERE MessagesID > '.$from:'';
  38. $ext = ($from)?' OFFSET '.$from:'';
  39. $q = sqlite_query($this->conn, "SELECT * FROM Messages LIMIT $count".$ext);
  40. return sqlite_fetch_all($q);
  41. }
  42.  
  43. public function delete( $id ) {
  44. if( !$this->connected )
  45. throw new Exception('Not connected to database.');
  46. sqlite_query($this->conn, "DELETE FROM Messages WHERE MessagesID=$id");
  47. }
  48. }
  49. //$gb = new GuestBook('book.db');
  50. //$gb->install();
  51. //$gb->addNewMark('Alex', '[email protected]', 5, 'Good!', '127.0.0.1');
  52. //print_r( $gb->getMessages(1,1) );
  53. //$gb->delete(1);
  54. ?>

Report this snippet  

You need to login to post a comment.