Advertisement
Guest User

Untitled

a guest
May 23rd, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. class dbconnect {
  2. protected $db_conn;
  3. public $db_user='xxxx';
  4. public $db_pass='xxxx';
  5. public $db_host='localhost';
  6. public $db_name='xxxx';
  7.  
  8. function connect() {
  9. try{
  10. $this->db_conn=new PDO("mysql:host=$this->db_host;dbname=$this->db_name",$this->db_user,$this->db_pass);
  11. return $this->db_conn;
  12. }
  13. catch (Exception $e){
  14. return $e->getMessage();
  15. }
  16. }
  17. }
  18.  
  19.  
  20. include_once ( 'class.dbconn.php' );
  21.  
  22. class Book{
  23. public $link;
  24.  
  25. public function __construct(){
  26. $db_conn=new dbconnect();
  27. $this->link = $db_conn->connect();
  28. return $this->link;
  29. }
  30. }
  31.  
  32. class new{
  33. include_once 'classes/class.book.php';
  34. $book = new Book();
  35. }
  36.  
  37. public function disconnect() {
  38. $this->db_conn = null
  39. }
  40.  
  41. public function __destruct() {
  42. $this->disconnect();
  43. }
  44.  
  45. <?php
  46. class dbconnect {
  47. protected $db_conn;
  48. public $db_user='xxxx';
  49. public $db_pass='xxxx';
  50. public $db_host='localhost';
  51. public $db_name='xxxx';
  52.  
  53. function connect() {
  54. try{
  55. $this->db_conn=new PDO("mysql:host=$this->db_host;dbname=$this->db_name",$this->db_user,$this->db_pass);
  56. return $this->db_conn;
  57. }
  58. catch (Exception $e){
  59. return $e->getMessage();
  60. }
  61. }
  62.  
  63. function disconnect()
  64. {
  65. try
  66. {
  67. $this->db_conn=null;
  68. return $this->db_conn;
  69. }
  70. catch (Exception $e){
  71. return $e->getMessage();
  72. }
  73. }
  74. }
  75. }
  76.  
  77.  
  78. include_once ( 'class.dbconn.php' );
  79.  
  80. class Book{
  81. public $link;
  82.  
  83. public function __construct(){
  84. $db_conn=new dbconnect();
  85. $this->link = $db_conn->connect();
  86. return $this->link;
  87. }
  88. }
  89.  
  90. class new{
  91. include_once 'classes/class.book.php';
  92. $book = new Book();
  93.  
  94. $close = new dbconnect();
  95. // this will close connection
  96. $close->disconnect();
  97.  
  98.  
  99. }?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement