Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Genesis Isle
- */
- class SQL{
- private $connection;
- // close connection - not neccessary, makes the script much slower. instead, i made a persistent connection which never
- // actually closes, even when the script ends. it is instead cached for future use.
- /*function closeConnection(){
- return 0;
- }*/
- // execute a query
- /*
- example:
- $result = SQL->query("SELECT name FROM people WHERE fname = :fname AND age = :age", array(':fname'=>$fname, ':age'=>$age));
- use the flags in cases where you use variables. for example, executing this would not be safe:
- "SELECT name FROM people WHERE fname = " . $fname . " AND age = " . $age
- instead, use the first example with flags. it avoids hackers from breaking into the code.
- you can also use question marks:
- $result = SQL->query("SELECT name FROM people WHERE fname = ? AND age = ?", array($fname, $age));
- returns data in an array.
- for example: $name1 = $result[0]['name']; name2 = $result[2]['name'];
- */
- public function __construct(){
- $this->connection = new PDO("mysql:host=" . MYSQL_HOST . ";dbname=" . MYSQL_DATABASE, MYSQL_UNAME, MYSQL_PASSWORD, array(PDO::ATTR_PERSISTENT => true));
- $this->connection->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
- }
- public function query($query, $array = NULL){
- $prep = $this->connection->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
- $prep -> execute($array);
- if ($prep->rowCount() == 0) {
- return false;
- }
- $result = $prep->fetchAll();
- return $result;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement