Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. <?php
  2. class Database {
  3.  
  4. private static $host = "localhost";
  5. private static $db_name = "c9";
  6. private static $username = "theandrewilliam";
  7. private static $password = "";
  8. private static $socket = "mysql";
  9.  
  10. //Instance of class
  11. private $instance = NULL;
  12.  
  13. //Connection to Database (predefined function)
  14. public function __construct(){
  15.  
  16. if($this -> instance == NULL){
  17. try{
  18.  
  19. $db = new PDO(self::$socket.':'.'host'.'='.self::$host.';'
  20. .'dbname'.'='.self::$db_name,self::$username,self::$password);
  21.  
  22. $this -> instance = $db;
  23.  
  24. } catch(PDOException $e){
  25.  
  26. die($e -> getMessage());
  27.  
  28. }
  29. }
  30.  
  31. }
  32.  
  33. //Queries
  34. public function query($sql){
  35.  
  36. $query = $this -> instance = prepare($sql);
  37. $query -> execute();
  38.  
  39. return $query;
  40.  
  41. }
  42.  
  43. }
  44.  
  45. $data = new Database();
  46.  
  47. $sq = 'SELECT * FROM body';
  48.  
  49. $result = $data -> query($sq);
  50.  
  51. while($row = $result -> $query -> fetch(PDO::FETCH_ASSOC)){
  52.  
  53. $body = $row['text'];
  54. echo $body;
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement