Guest User

Untitled

a guest
Jul 28th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Andmebaasi class.
  5. */
  6.  
  7. class Database {
  8. private $user = 'aaa'; // Kasutajanimi
  9. private $pass = 'aaa'; // Parool
  10. private $server = 'localhost'; // MySQL server
  11. private $database = 'aaaa'; // Andmebaasi nimi
  12. private $con; // Ühendus andmebaasiga
  13.  
  14. // Automaatselt connectib andmebaasiga peale objekti teket
  15. function __construct() {
  16. $this->con = mysql_connect($this->server, $this->user, $this->pass);
  17. if(!$this->con) {
  18. die('Mysql ühendus ebaõnnestus: ' . mysql_error());
  19. } else {
  20. $dbSelect = mysql_select_db($this->database, $this->con);
  21. if(!$dbSelect) {
  22. die('Andmebaasi valik ebaõnnestus: ' . mysql_error());
  23. }
  24. }
  25. }
  26.  
  27. // Andmebaasi ühenduse sulgemine
  28. public function closeCon() {
  29. if(isset($this->con)) {
  30. mysql_close($this->con);
  31. unset($this->con);
  32. }
  33. }
  34.  
  35. // Andmebaasi päringu tegemine
  36. public function query($sql) {
  37. $result = mysql_query($sql, $this->con);
  38. if(!$result) {
  39. die('Päring ebaõnnestus: ' . mysql_error());
  40. }
  41. return $result;
  42. }
  43.  
  44. // Valmistab andmebaasi jaoks
  45. public function escVal($value) {
  46. $value = mysql_real_escape_string($value);
  47. return $value;
  48. }
  49.  
  50. // Teeb andmebaasist tuleva data kasutatavaks
  51. public function fetchArray($result) {
  52. return mysql_fetch_array($result);
  53. }
  54.  
  55. // Annab andmebaasi päringu ridade arvu
  56. public function numRows($result) {
  57. return mysql_num_rows($result);
  58. }
  59. }
  60.  
  61. // Tekitab db objekti automaatselt, kui db_class.php kuhugi tiritakse
  62. $db = new Database();
Add Comment
Please, Sign In to add comment