Advertisement
Guest User

Untitled

a guest
May 11th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. class bbddConnection {
  2.  
  3.  
  4. private $_host = 'localhost';
  5. private $_username = 'username';
  6. private $_password = 'password';
  7. private $_db_name = 'DBNAME';
  8. private $_mysqli = NULL;
  9. private static $_instance = NULL;
  10.  
  11. private function __construct() {
  12. $this->connection();
  13. }
  14.  
  15.  
  16. public static function get_instance() {
  17. if (!isset(self::$_instance)) {
  18. //echo "Creating new instance.n";
  19. $class_name = __CLASS__;
  20. self::$_instance = new $class_name;
  21. }
  22. return self::$_instance;
  23. }
  24.  
  25.  
  26. private function connection() {
  27. //echo "Creating new connection.n";
  28. try {
  29. //The mysqli object will be created once and only once
  30. if (!$this->_mysqli instanceof mysqli) {
  31. $this->_mysqli = new mysqli($this->_host, $this->_username, $this->_password);
  32. }
  33. if ($this->_mysqli->connect_errno) {
  34. throw new Exception('An error occured: ' . $this->_mysqli->connect_error);
  35. } else {
  36. $this->select_db();
  37. }
  38. } catch (Exception $e) {
  39. echo $e->getMessage() . "n";
  40. }
  41. }
  42.  
  43. private function select_db() {
  44. //echo "Selecting database.n";
  45. try {
  46. $this->_mysqli->select_db($this->_db_name) or die('Could not find database');
  47. } catch (Exception $e) {
  48. echo $e->getMessage() . "n";
  49. }
  50. }
  51.  
  52. public function query($query) {
  53. if (!isset($query))
  54. return;
  55. //echo "Prevent SQL Injection. n"
  56. $query = $this->_mysqli->real_escape_string($query);
  57. return $this->_mysqli->query($query);
  58. }
  59.  
  60. public function stmt_init() {
  61. return $this->_mysqli->stmt_init();
  62. }
  63.  
  64.  
  65. public function __destruct() {
  66. if ($this->_mysqli instanceof mysqli) {
  67. $this->_mysqli->close();
  68. }
  69. }
  70.  
  71. private function __clone() {
  72. trigger_error('Clone is not allowed.', E_USER_ERROR);
  73. }
  74.  
  75. private function __wakeup() {
  76. trigger_error('Unserializing is not allowed.', E_USER_ERROR);
  77. }
  78. }
  79.  
  80. class gestorUsuarios {
  81.  
  82. private $db;
  83.  
  84. public function __construct() {
  85. $this->db = bbddConnection::get_instance();
  86. }
  87. /**
  88. * Returns the Name of the user according his ID
  89. * @param int id
  90. * @return string Nombre, if an error occurred.
  91. * returns <b>FALSE</b>
  92. */
  93. function getUserName($id)
  94. {
  95.  
  96. $stmt = $this->db->stmt_init();
  97. if ($stmt->prepare("Select Nombre from usuarios where id = ?")) {
  98. $name = '';
  99. $stmt->bind_param('i', $id);
  100. $stmt->execute();
  101. $stmt->bind_result($name);
  102.  
  103. while ($stmt->fetch())
  104. $nombre = $name;
  105. $stmt->close();
  106.  
  107. return $nombre;
  108. } else {
  109. return false;
  110. }
  111. }
  112. /**
  113. * Edit the Name of the user according his ID
  114. * @param String Nombre, int id
  115. * @return Boolean if an error occurred.
  116. * returns <b>FALSE</b>, else returns <b>TRUE</b>
  117. */
  118. function setUserName($nombre, $id)
  119. {
  120.  
  121. $stmt = $this->db->stmt_init();
  122. if ($stmt->prepare("UPDATE usuarios SET Nombre=? WHERE id =?")) {
  123.  
  124. $stmt->bind_param('si', $nombre, $id);
  125. $stmt->execute();
  126. $stmt->close();
  127.  
  128. return true;
  129. } else {
  130. return false;
  131. }
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement