Guest User

Untitled

a guest
Aug 4th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.39 KB | None | 0 0
  1. <?php
  2. /** last modified 22032012 */
  3.  
  4. define ("DB_ADDRESS", "localhost");
  5. define ("DB_USERNAME", "kalaPuikko_user");
  6. define ("DB_PASSWORD", "12345");
  7. define ("DB_NAME", "kalaPuikko");
  8.  
  9. class databaseInterface {
  10.  
  11. private $echo = false; // a boolean for printi$query = "SELECT * FROM users WHERE username='$user' && password='$pass'";
  12. private $db_link = null; // the sql database link
  13. private $db_username = null; // username for sql connection
  14. private $db_password = null; // password for sql connection
  15. private $db_name = null; // name of the selected database
  16. private $db_address = null; // address of the database i.e. localhost or 127.0.0.1
  17. private $query = null; // the sql query to be performed i.e. "show tables" or "select * from <tbl_name>"
  18. private $result = null; // results of the sql query
  19. private $transaction_active = null; // a boolean which indicates whether a transaction has already been started or not
  20.  
  21. /**
  22. Constructor opens a sql connection to the specified location with the given username and password.
  23. It also selects the given sql database.
  24.  
  25. @db_address is the address (url) of the database
  26. @db_username is the name of the user for the database connection
  27. @db_password is the password for the database connection
  28. @db_name is the name of the database to be selected
  29. */
  30. public function __construct($db_address = DB_ADDRESS, $db_username = DB_USERNAME, $db_password = DB_PASSWORD, $db_name = DB_NAME)
  31. {
  32. $this->db_address = $db_address;
  33. $this->db_username = $db_username;
  34. $this->db_password = $db_password;
  35. $this->db_name = $db_name;
  36. $this->db_link = mysql_connect($this->db_address,$this->db_username,$this->db_password) or die('Could not connect: ' . mysql_error());
  37. mysql_select_db($db_name) or die('Could not select database');
  38. }
  39.  
  40.  
  41. /**
  42. Destructor closes the sql connection automatically.
  43. PHP does this efficiently all by itself.
  44. */
  45. public function __destruct()
  46. {
  47. // mysql_close($this->db_link);
  48. }
  49.  
  50.  
  51. /**
  52. setEchoOn function sets the $echo variable as true and then messages for debugging purposes are printed out
  53. */
  54. public function setEchoOn()
  55. {
  56. $echo = true;
  57. }
  58.  
  59.  
  60. /**
  61. connectoToDatabase function opens a sql connection to the specified location with the given username and password.
  62.  
  63. @db_address is the address (url) of the database
  64. @db_username is the name of the user for the database connection
  65. @db_password is the password for the database connection
  66. */
  67. public function connectToDatabase($db_address, $db_username, $db_password)
  68. {
  69. $this->db_address = $db_address;
  70. $this->db_username = $db_username;
  71. $this->db_password = $db_password;
  72. $this->db_link = mysql_connect($this->db_address,$this->db_username,$this->db_password) or die('Could not connect: ' . mysql_error());
  73. if ($this->echo) print("connected to database succesfully<BR>");
  74. }
  75.  
  76. /**
  77. selectDatabase function selects the given sql database.
  78.  
  79. @db_name is the name of the database to be selected
  80. */
  81. public function selectDatabase($db_name)
  82. {
  83. $this->$db_name = $db_name;
  84. mysql_select_db($db_name) or die('Could not select database');
  85. if ($this->echo) print("selected database ".$db_name." succesfully<BR>");
  86. }
  87.  
  88.  
  89. /*
  90. performSelectQuery function performs a select type query.
  91.  
  92. @fields string contains the fields names of the table e.g. 'username' or 'id, username, password' or '*'
  93. @tbl_name name of the table that fields are selected from
  94. @condition string contains any further instruction e.g. 'ORDER BY username' or WHERE id = "'1'".
  95. */
  96. public function performSelectQuery($fields, $tbl_name, $condition)
  97. {
  98. $this->query = 'SELECT '.$fields.' FROM '.$tbl_name.' '.$condition;
  99. $this->result = mysql_query($this->query) or die('Query failed: ' . mysql_error());
  100. if ($this->echo) print("sql query completed succesfully<BR>");
  101. return $this->result;
  102. }
  103.  
  104.  
  105. /*
  106. performGetContent function gets single field from desired content.
  107.  
  108. @fields string contains the fields names of the table e.g. 'username' or 'id, username, password' or '*'
  109. @tbl_name name of the table that fields are selected from
  110. @condition string contains any further instruction e.g. 'ORDER BY username' or WHERE id = "'1'".
  111. @data name of the data you want to get from selected field e.g. 'password'
  112. */
  113. public function performGetContent($fields, $tbl_name, $condition, $data)
  114. {
  115. $this->query = 'SELECT '.$fields.' FROM '.$tbl_name.' '.$condition;
  116. $this->result = mysql_query($this->query) or die ('Query failed: ' . mysql_error());
  117. $this->data = mysql_fetch_object($this->result);
  118. $this->result = ($this->data->$data);
  119. if ($this->echo) print($this->result);
  120. return $this->result;
  121. }
  122.  
  123.  
  124. /*
  125. performInsertQuery function performs a insert type query.
  126.  
  127. @tbl_name name of the table(s), where new information will be stored to
  128. @fields string contains the fields names of the table where information will be stored to i.e. 'username, password'
  129. @values string contains the values to be stored to the specified fields e.g. '"newuser" ,"newpassword"'
  130. */
  131. public function performInsertQuery($tbl_name, $fields, $values)
  132. {
  133. $this->query = 'INSERT INTO '.$tbl_name.' ('.$fields.') values ('.$values.')';
  134. if ($this->echo) print($this->query."<BR>");
  135. $this->result = mysql_query($this->query) or die('Query failed: ' . mysql_error());
  136. if ($this->echo) print("sql query completed succesfully<BR>");
  137. }
  138.  
  139.  
  140. /**
  141. perfornUpdateQuery function performs a update type query.
  142.  
  143. @tbl_name name of the table(s) whose fields will be updated
  144. @fields an array which contains the fields names that will be updated
  145. @values an array which contains the values to the specified fields
  146. @condition string contains any further instruction e.g. 'ORDER BY username' or WHERE id = "'1'".
  147. */
  148. public function performUpdateQuery($tbl_name, $fields, $values, $condition)
  149. {
  150. $this->query = 'UPDATE '.$tbl_name.' SET ';
  151. for ($i=0; $i<count($fields); $i++)
  152. {
  153. $this->query = $this->query.$fields[$i]." = \"".$values[$i]."\" ";
  154. if ($i < (count($fields) -1))
  155. {
  156. $this->query = $this->query.", ";
  157. }
  158. }
  159. $this->query = $this->query.$condition;
  160. $this->result = mysql_query($this->query) or die('Query failed: ' . mysql_error());
  161. if ($this->echo) print("sql query completed succesfully<BR>");
  162. }
  163.  
  164.  
  165. /**
  166. perfornDeleteQuery function performs a delete type query.
  167.  
  168. @tbl_name name of the table(s) whose row(s) will be deleted
  169. @condition string contains the condition on which the rows will be deleted from the table e.g. WHERE id = "'1'".
  170. */
  171. public function performDeleteQuery($tbl_name, $condition)
  172. {
  173. $this->query = 'DELETE '.$tbl_name.' FROM '.$tbl_name.' '.$condition;
  174. $this->result = mysql_query($this->query) or die('Query failed: ' . mysql_error());
  175. if ($this->echo) print("sql query completed succesfully<BR>");
  176. }
  177.  
  178.  
  179. /**
  180. performs any the given string as a MYSQL query.
  181.  
  182. $query is the MYSQL query
  183.  
  184. returns FALSE, if query failed
  185. returns TRUE, if query was succesful
  186. */
  187. public function query($query)
  188. {
  189. $this->query = $query;
  190. $this->result = mysql_query($this->query);
  191. // echo $query;
  192. // print_r($this->result);
  193. if ($this->result === FALSE) die(mysql_error());
  194. // if (!$this->result) return array(FALSE,mysql_error());
  195. else return $this->result;
  196. }
  197.  
  198.  
  199. /**
  200. begins a new transaction, which is a series of queries. the queries will be permanent after commit function is called and before that changes can be cancelled by calling rollback function.
  201. */
  202. public function startTransaction()
  203. {
  204. $this->result = mysql_query("START TRANSACTION") or die('Query failed: ' . mysql_error());
  205. $this->transaction_active = TRUE;
  206. }
  207.  
  208.  
  209. /**
  210. ends a transction and makes the changes permanent
  211. */
  212. public function commit()
  213. {
  214. $this->result = mysql_query("COMMIT") or die('Query failed: ' . mysql_error());
  215. $this->transaction_active = FALSE;
  216. }
  217.  
  218.  
  219. /**
  220. ends a transction and cancels the changes
  221. */
  222. public function rollback()
  223. {
  224. $this->result = mysql_query("ROLLBACK") or die('Query failed: ' . mysql_error());
  225. $this->transaction_active = FALSE;
  226. }
  227.  
  228.  
  229. /**
  230. returns a true if transaction is active and false if not
  231. */
  232. function isTransactionActive()
  233. {
  234. return $this->transaction_active;
  235. }
  236.  
  237.  
  238. /**
  239. returns the MYSQL query's next row as an array by mysql association
  240. */
  241. public function getLinesAssoc()
  242. {
  243. return mysql_fetch_array($this->result, MYSQL_ASSOC);
  244. }
  245.  
  246. public function getLinesAssocArray()
  247. {
  248. $array=array();
  249. $resultRows=mysql_num_rows($this->result);
  250. for ($i=0;$i<$resultRows;$i++){
  251. $array[]=mysql_fetch_array($this->result, MYSQL_ASSOC);
  252.  
  253. }
  254. return $array;
  255. }
  256.  
  257.  
  258. /**
  259. returns the MYSQL query's next row as an array in indexed format
  260. */
  261. public function getLinesIndexed()
  262. {
  263. return mysql_fetch_array($this->result, MYSQL_NUM);
  264. }
  265.  
  266.  
  267. /*
  268. getLastId() fucntion returns the id value of last insert query performed.
  269.  
  270. @id the value of the id field
  271. */
  272. public function getLastId()
  273. {
  274. $id = mysql_insert_id();
  275. return $id;
  276. }
  277.  
  278.  
  279. /*
  280. isResultEmpty function returns true if the result set is empty, otherwise it will return false.
  281. */
  282. public function isResultEmpty()
  283. {
  284. if (mysql_num_rows($this->result) === 0)
  285. {
  286. return true;
  287. }
  288. else
  289. {
  290. return false;
  291. }
  292. }
  293.  
  294.  
  295. /*
  296. getNumberOfResults function returns the amount of result rows in the result set.
  297. */
  298. public function getNumberOfResults()
  299. {
  300. if ($this->result === FALSE) {
  301. return "ERROR";
  302. }
  303. else {
  304. return mysql_num_rows($this->result);
  305. }
  306. }
  307.  
  308.  
  309. /*
  310. showQueryResults function displays the results of the sql query on screen in HTML format.
  311. */
  312. public function showQueryResults()
  313. {
  314. echo "<table>\n";
  315. while ($line = mysql_fetch_array($this->result, MYSQL_ASSOC))
  316. {
  317. echo "\t<tr>\n";
  318. foreach ($line as $col_value)
  319. {
  320. echo "\t\t<td>$col_value</td>\n";
  321. }
  322. echo "\t</tr>\n";
  323. }
  324. echo "</table>\n";
  325. }
  326.  
  327.  
  328. /**
  329. Get number of affected rows in previous SQL operation.
  330.  
  331. This function returns the number of rows affected by last
  332. INSERT, UPDATE or DELETE statement. For SELECT statements, see
  333. getNumberOfResults.
  334.  
  335. @return Number of affected rows
  336. */
  337. public function getNumberOfAffectedRows() // the slow way to do a simple thing. Pure lazyness.
  338. {
  339. return mysql_affected_rows( $this->db_link );
  340. }
  341.  
  342. public function escape( $string ) // the slow and complicated way to do a simple thing.
  343. {
  344. if ( get_magic_quotes_gpc() )
  345. {
  346. $string = stripslashes( $string );
  347. }
  348.  
  349. if(function_exists('mysql_real_escape_string'))
  350. {
  351. return mysql_real_escape_string($string);
  352. }
  353. elseif(function_exists('mysql_escape_string'))
  354. {
  355. return mysql_escape_string($string);
  356. }
  357. else
  358. {
  359. return addslashes($string);
  360. }
  361. }
  362.  
  363. } // end of class
  364.  
  365. ?>
Add Comment
Please, Sign In to add comment