Advertisement
Guest User

inc/Database.php

a guest
Apr 19th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. <?php
  2. class database{
  3.  
  4. public $server = "";
  5. public $user = "";
  6. protected $dbName;
  7.  
  8. protected $password = "";
  9.  
  10. public $error = array();
  11.  
  12. protected $conn;
  13.  
  14. public function __construct($server, $user, $password, $databaseName){ // change this later to be more efficient
  15.  
  16. // check if the server is set
  17. if($server == "")
  18. {
  19. $this->error[] = "server is empty";
  20. }
  21.  
  22. // check if the password is set
  23. if($user == "")
  24. {
  25. $this->error[] = "user is empty";
  26. }
  27. // check if the server is set
  28. if($password == "")
  29. {
  30. $this->error[] = "password is empty";
  31. }
  32.  
  33. // check if the password is set
  34. if($databaseName == "")
  35. {
  36. $this->error[] = "databaseName is empty";
  37. }
  38.  
  39.  
  40. $this->server = $server;
  41. $this->user = $user;
  42. $this->password = $password;
  43. $this->dbName = $databaseName;
  44.  
  45.  
  46. }
  47.  
  48. function conn(){ // create a connection with the database
  49. $conn = mysqli_connect($this->server, $this->user, $this->password, $this->dbName);// database connection
  50. return $conn;
  51. }
  52.  
  53. function select($colum, $table){ //a select function to make it easy to select rows from the database
  54. $this->conn = $this->conn(); // make a new connection
  55. //maak verbinding
  56. //SELECT name FROM landen
  57. $sql = "SELECT ".$colum." FROM ". $table; //make a string containing the sql
  58. $query = mysqli_query($this->conn, $sql); //execute the sql
  59. mysqli_close($this->conn); // cloe the connection.
  60. //this makes it harder for people to intercept the communication between the server and the client(this includes scripts and such)
  61. return $query; // return the result to the place where you execute it
  62. }
  63.  
  64. function selectWhere($colum, $table, $where){
  65. $this->conn = $this->conn();
  66. // SELECT name FROM landen WHERE landID=3
  67. $sql = "SELECT ".$colum." FROM ".$table." WHERE ".$where;
  68. $query = mysqli_query($this->conn, $sql);
  69. mysqli_close($this->conn);
  70. return $query;
  71. }
  72.  
  73. function insert($table, $colums,$values){ /// NOT DONE CHANGE THE CONNS TO $THIS YOU DIP
  74. $this->conn = conn();
  75. // INSERT INTO landen (name) VALUES (Duitsland)
  76. $sql = "INSERT INTO ".$table." (".$colums.") VALUES (".$values.")";
  77.  
  78. // INTERT INTO producten (type,bouwjaar,prijs,image)
  79. // VALUES ("A4")
  80. mysqli_query($conn, $sql);
  81. $lastID = mysqli_insert_id($conn); // EXTRA VOOR DEZE OPDRACHT
  82. mysqli_close($conn);
  83. return $lastID;
  84. }
  85.  
  86. // make a function to insert an array into the database. work on this in the near futer
  87. /* function insertArray($array, $table)
  88. {
  89. $this->conn = conn();
  90.  
  91. foreach($array)
  92.  
  93. // INSERT INTO landen (name) VALUES (Duitsland)
  94. $sql = "INSERT INTO ".$table." (".$colums.") VALUES (".$values.")";
  95.  
  96. // INTERT INTO producten (type,bouwjaar,prijs,image)
  97. // VALUES ("A4")
  98. mysqli_query($conn, $sql);
  99. $lastID = mysqli_insert_id($conn); // EXTRA VOOR DEZE OPDRACHT
  100. mysqli_close($conn);
  101. return $lastID;
  102.  
  103. }*/
  104. }
  105. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement