Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. * Genesis Isle
  5. */
  6.  
  7. class SQL{
  8. private $connection;
  9.  
  10. // close connection - not neccessary, makes the script much slower. instead, i made a persistent connection which never
  11. // actually closes, even when the script ends. it is instead cached for future use.
  12. /*function closeConnection(){
  13. return 0;
  14. }*/
  15.  
  16. // execute a query
  17. /*
  18. example:
  19. $result = SQL->query("SELECT name FROM people WHERE fname = :fname AND age = :age", array(':fname'=>$fname, ':age'=>$age));
  20. use the flags in cases where you use variables. for example, executing this would not be safe:
  21. "SELECT name FROM people WHERE fname = " . $fname . " AND age = " . $age
  22. instead, use the first example with flags. it avoids hackers from breaking into the code.
  23. you can also use question marks:
  24. $result = SQL->query("SELECT name FROM people WHERE fname = ? AND age = ?", array($fname, $age));
  25. returns data in an array.
  26. for example: $name1 = $result[0]['name']; name2 = $result[2]['name'];
  27. */
  28. public function __construct(){
  29. $this->connection = new PDO("mysql:host=" . MYSQL_HOST . ";dbname=" . MYSQL_DATABASE, MYSQL_UNAME, MYSQL_PASSWORD, array(PDO::ATTR_PERSISTENT => true));
  30. $this->connection->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  31. }
  32.  
  33. public function query($query, $array = NULL){
  34. $prep = $this->connection->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
  35. $prep -> execute($array);
  36. if ($prep->rowCount() == 0) {
  37. return false;
  38. }
  39. $result = $prep->fetchAll();
  40. return $result;
  41. }
  42. }
  43.  
  44. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement