Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Connect to the database using PDO and MySQL
- class DbConnect extends PDO
- {
- protected $dbPassword = '';
- protected $dbUsrName = '';
- protected $opt = array(
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
- );
- function __construct()
- {
- parent::__construct('mysql:host=localhost;dbname=DBNAME;', 'user', 'password', $this->opt);
- echo "Connected to Database";
- }
- var $stmt;
- var $result_set;
- function execQuery($query, $values, $return)
- {
- // prepares passed in query, query must use "?" as placeholders for values
- $this->stmt = parent::prepare($query);
- // loops through $values array, check each value's type and binds it as such
- foreach($values as $i => $value)
- {
- $j = $i + 1;
- if (is_int($value))
- $this->stmt->bindValue($j, $value, PDO::PARAM_INT);
- else if (is_bool($value))
- $this->stmt->bindValue($j, $value, PDO::PARAM_BOOL);
- else if (is_string($value))
- $this->stmt->bindValue($j, $value, PDO::PARAM_STR);
- }
- if (!$this->stmt->execute())
- return false;
- else if ($return)
- $this->result_set = $this->stmt->fetchAll();
- }
- function CloseConnection()
- {
- $this->DbConnection = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment