Guest User

Untitled

a guest
Jun 11th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. <?php
  2. class DBConfig {
  3.  
  4. var $host;
  5. var $user;
  6. var $pass;
  7. var $db;
  8. var $db_link;
  9. var $conn = false;
  10. var $persistant = false;
  11.  
  12. public $error = false;
  13.  
  14. public function config(){ // class config
  15. $this->error = true;
  16. $this->persistant = false;
  17. }
  18.  
  19. function conn($host='localhost',$user='user',$pass='123456',$db='orgp'){ // connection function
  20. $this->host = $host;
  21. $this->user = $user;
  22. $this->pass = $pass;
  23. $this->db = $db;
  24.  
  25. // Establish the connection.
  26. if ($this->persistant)
  27. $this->db_link = mysql_pconnect($this->host, $this->user, $this->pass, true);
  28. else
  29. $this->db_link = mysql_connect($this->host, $this->user, $this->pass, true);
  30.  
  31. if (!$this->db_link) {
  32. if ($this->error) {
  33. $this->error($type=1);
  34. }
  35. return false;
  36. }
  37. else {
  38. if (empty($db)) {
  39. if ($this->error) {
  40. $this->error($type=2);
  41. }
  42. }
  43. else {
  44. $db = mysql_select_db($this->db, $this->db_link); // select db
  45. if (!$db) {
  46. if ($this->error) {
  47. $this->error($type=2);
  48. }
  49. return false;
  50. }
  51. $this -> conn = true;
  52. }
  53. return $this->db_link;
  54. }
  55. }
  56.  
  57. function close() { // close connection
  58. if ($this -> conn){ // check connection
  59. if ($this->persistant) {
  60. $this -> conn = false;
  61. }
  62. else {
  63. mysql_close($this->db_link);
  64. $this -> conn = false;
  65. }
  66. }
  67. else {
  68. if ($this->error) {
  69. return $this->error($type=4);
  70. }
  71. }
  72. }
  73.  
  74. public function error($type=''){ //Choose error type
  75. if (empty($type)) {
  76. return false;
  77. }
  78. else {
  79. if ($type==1)
  80. echo "<strong>Database could not connect</strong> ";
  81. else if ($type==2)
  82. echo "<strong>mysql error</strong> " . mysql_error();
  83. else if ($type==3)
  84. echo "<strong>error </strong>, Proses has been stopped";
  85. else
  86. echo "<strong>error </strong>, no connection !!!";
  87. }
  88. }
  89. }
  90.  
  91. // example to use
  92.  
  93. $DB = new DBConfig();
  94. $DB -> config();
  95. $DB -> conn();
  96. if( isset( $_POST['ajax'] ) ){
  97. mysql_query('INSERT INTO `notes` (`number`,`date`,`subj`,`status`) values ("'. mysql_real_escape_string($_POST['number']) .'", "'. mysql_real_escape_string($_POST['date']) .'","'. mysql_real_escape_string($_POST['subject']) .'","'. mysql_real_escape_string($_POST['st']) .'");');
  98. }
  99. $DB -> close();
  100. ?>
Add Comment
Please, Sign In to add comment