Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class mysql extends phpSite
- {
- var $DBHost,$DBUser,$DBPass,$DBPort,$DBConn,$DBName,$DBTables;
- //constructor for mysql class, also the mysql connector
- function mysql ($DBHost,$DBUser,$DBPass,$DBName,$DBPort=3306)
- {
- try
- {
- $this->DBConn = new mysqli($DBHost,$DBUser,$DBPass,$DBName,$DBPort);
- if ($this->DBConn->connect_error)
- throw new Exception("Error occurred (".$this->DBConn->connect_errno."): ".$this->DBConn->connect_error,100);
- }
- catch (Exception $e)
- {
- $this->ErrorHandler($e);
- }
- }
- //Function to return query as an object or array
- public function query($sql,$returnType = 1)
- {
- $query = $this->DBConn->query($sql);
- $num = @$query->num_rows;
- if ($num>0)
- {
- $record = array();
- if ($returnType == 1)
- {
- while ($rs=$query->fetch_object())
- {
- $record[] = $rs;
- }
- $record = (object)$record;
- }
- else
- {
- while ($rs=$query->fetch_array())
- {
- $record[] = $rs;
- }
- }
- return $record;
- }
- else
- {
- return array();
- }
- }
- //Function to close mysql connection. Will call destructor, since class can't be used without mysql-connection..
- function close()
- {
- unset($this);
- }
- //Destructor - called when class is destroyed.
- function __destruct()
- {
- try
- {
- if ($this->DBConn->close() === false)
- throw new Exception("Could not close connection to database server.
- Does connection exist?",101);
- }
- catch (Exception $e)
- {
- $this->ErrorHandler($e);
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement