Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Support for MySQL, MySQLi, MS SQL & PostgreSQL
- *
- * @author Richard Rowe <inbox@richardrowe.net>
- *
- */
- /**
- * @todo Improve error handling
- * @todo Some bug fixing / testing in mysqli class
- * @todo Test MS SQL, MySQL & PostgreSQL classes
- */
- interface db_base
- {
- public function __construct($host = null, $user = null, $pass = null, $dbname = null);
- public function close();
- public function affected_rows();
- public function fetchrow($result = false);
- public function freeresult($result = false);
- public function insert_id();
- public function num_rows($result = false);
- public function query($sql);
- public function escape_string($str);
- public function error($str);
- }
- class db_mssql implements db_base
- {
- public $link;
- public $result;
- public $error;
- public function __construct($host = null, $user = null, $pass = null, $dbname = null)
- {
- /**
- * Open MS SQL server connection
- */
- if (!$this->link = mssql_connect($host, $user, $pass))
- {
- // error!
- trigger_error('Could not connect to the database.', E_USER_ERROR);
- }
- /**
- * Select MS SQL database
- */
- if (!mssql_select_db($dbname, $this->link))
- {
- // error!
- trigger_error('Could not select the database.', E_USER_ERROR);
- }
- }
- /**
- * Close MS SQL Server connection
- */
- public function close()
- {
- if (!mssql_close($this->link))
- {
- // error!
- trigger_error('Could not close the database.', E_USER_ERROR);
- }
- return true;
- }
- /**
- * Returns the number of records affected by the query
- */
- public function affected_rows()
- {
- return $this->link ? mssql_rows_effected($this->link) : false;
- }
- /**
- * Get row as enumerated array
- */
- public function fetchrow($result = false)
- {
- return $result ? mssql_fetch_assoc($result) : mssql_fetch_assoc($this->result);
- }
- /**
- * Free result memory
- */
- public function freeresult($result = false)
- {
- return $result ? mssql_free_result($result) : mssql_free_result($this->result);
- }
- /**
- * Method taken from phpBB.
- */
- public function insert_id()
- {
- if ($result = mssql_query('SELECT SCOPE_IDENTITY()', $this->link))
- {
- if ($row = mssql_fetch_assoc($result))
- {
- mssql_free_result($result);
- return $row['computed'];
- }
- mssql_free_result($result);
- }
- return false;
- }
- /**
- * Gets the number of rows in result
- */
- public function num_rows($result = false)
- {
- return $result ? mssql_num_rows($result) : mssql_num_rows($this->result);
- }
- /**
- * Send MS SQL query
- */
- public function query($sql)
- {
- if (!$this->result = mssql_query($sql, $this->link))
- {
- // error!
- //trigger_error('<strong>Query error:</strong><br />', E_USER_ERROR);
- }
- return $this->result;
- }
- /**
- * Escapes special characters in a string for use in a SQL statement
- *
- * @param string $str The string that is to be escaped.
- * @return string Returns the escaped string.
- */
- public function escape_string($str)
- {
- // Magic Quotes escapes all incoming data to the PHP script.
- if (ini_get('magic_quotes_gpc'))
- {
- // To avoid escaping the string twice, stripslashes
- $str = stripslashes($str);
- }
- return str_replace('\'', '\'\'', $str);
- }
- public function error($str)
- {
- return $this->error = $str;
- }
- }
- /**
- * @todo public function insert_id()
- */
- class db_postgresql implements db_base
- {
- public $link;
- public $result;
- public $error;
- public function __construct($host = null, $user = null, $pass = null, $dbname = null)
- {
- if (!$this->link = pg_connect("host = $host, dbname = $dbname, user = $user, password = $pass"))
- {
- // error!
- trigger_error('Could not connect and / or select the database.', E_USER_ERROR);
- }
- }
- /**
- * Closes a PostgreSQL connection
- */
- public function close()
- {
- if (!pg_close($this->link))
- {
- // error!
- trigger_error('Could nto close the database.', E_USER_ERROR);
- }
- return true;
- }
- /**
- * Returns number of affected records (tuples)
- */
- public function affected_rows()
- {
- return $this->link ? pg_affected_rows($this->link) : false;
- }
- /**
- * Fetch a row as an associative array
- */
- public function fetchrow($result = false)
- {
- return $result ? pg_fetch_assoc($result) : pg_fetch_assoc($this->result);
- }
- /**
- * Free result memory
- */
- public function freeresult($result = false)
- {
- return $result ? pg_free_result($result) : pg_free_result($this->result);
- }
- /**
- *
- */
- public function insert_id()
- {
- //return pg_last_oid($this->link);
- }
- /**
- * Returns the number of rows in a result
- */
- public function num_rows($result = false)
- {
- return $result ? pg_num_rows($result) : pg_num_rows($this->result);
- }
- /**
- * Execute a query
- */
- public function query($sql)
- {
- if (!$this->result = pg_query($this->link, $sql))
- {
- // error!
- trigger_error('<strong>Query failed:</strong><br />' . pg_result_error($this->result), E_USER_ERROR);
- }
- return $this->result;
- }
- /**
- * Escape a string for insertion into a text field
- *
- * @param string $str A string containing text to be escaped.
- * @return A string containing the escaped data.
- */
- public function escape_string($str)
- {
- // Magic Quotes escapes all incoming data to the PHP script.
- if (ini_get('magic_quotes_gpc'))
- {
- // To avoid escaping the string twice, stripslashes
- $str = stripslashes($str);
- }
- return pg_escape_string($this->link, $str);
- }
- public function error($str)
- {
- return $this->error = $str;
- }
- }
- class db_mysql implements db_base
- {
- public $link;
- public $result;
- public $error;
- public function __construct($host = null, $user = null, $pass = null, $dbname = null)
- {
- // Open a connection to a MySQL Server
- if (!$this->link = mysql_connect($host, $user, $pass, true))
- {
- trigger_error('<strong>Could not connect to the database:</strong><br />' . mysql_error(), E_USER_ERROR);
- }
- // Select a MySQL database
- if (!mysql_select_db($dbname, $this->link))
- {
- trigger_error('<strong>Could not select the database:</strong><br />' . mysql_error(), E_USER_ERROR);
- }
- }
- /**
- * Close MySQL connection
- */
- public function close()
- {
- if (!mysql_close($this->link))
- {
- // error!
- trigger_error('Could not close the database.', E_USER_ERROR);
- }
- return true;
- }
- /**
- * Get number of affected rows in previous MySQL operation
- */
- public function affected_rows()
- {
- return $this->link ? mysql_affected_rows($this->link) : false;
- }
- /**
- * Fetch a result row as an associative array
- *
- * @param resource $result The result resource that is being evaluated. This result comes from a call to mysql_query().
- */
- public function fetchrow($result = false)
- {
- return $result ? mysql_fetch_assoc($result) : mysql_fetch_assoc($this->result);
- }
- /**
- * Free result memory
- *
- * @param resource $result The result resource that is being evaluated. This result comes from a call to mysql_query().
- */
- public function freeresult($result = false)
- {
- return $result ? mysql_free_result($result) : mysql_free_result($this->result);
- }
- /**
- * Get the ID generated from the previous INSERT operation
- */
- public function insert_id()
- {
- return $this->link ? mysql_insert_id($this->link) : false;
- }
- /**
- * Get number of rows in result
- *
- * @param resource $result The result resource that is being evaluated. This result comes from a call to mysql_query().
- */
- public function num_rows($result = false)
- {
- return $result ? mysql_num_rows($result) : mysql_num_rows($this->result);
- }
- /**
- * Send a MySQL query
- *
- * @param string $sql A SQL query. The query string should not end with a semicolon.
- */
- public function query($sql)
- {
- if (!$this->result = mysql_db_query($sql, $this->link))
- {
- // error!
- trigger_error('<strong>Query error:</strong><br />' . mysql_error($this->link), E_USER_ERROR);
- }
- return $this->result;
- }
- /**
- * Escapes special characters in a string for use in a SQL statement
- *
- * @param string $str The string that is to be escaped.
- * @return string Returns the escaped string, or FALSE on error.
- */
- public function escape_string($str)
- {
- // Magic Quotes escapes all incoming data to the PHP script.
- if (ini_get('magic_quotes_gpc'))
- {
- // To avoid escaping the string twice, stripslashes
- $str = stripslashes($str);
- }
- return mysql_real_escape_string($str, $this->link);
- }
- public function error($str)
- {
- return $this->error = $str;
- }
- }
- class db_mysqli implements db_base
- {
- public $link;
- public $result;
- public $errors;
- public function __construct($host = null, $user = null, $pass = null, $dbname = null)
- {
- if (!$this->link = new mysqli($host, $user, $pass, $dbname))
- {
- // error!
- trigger_error('Could not connect to the database', E_USER_ERROR);
- }
- }
- /**
- * Closes a previously opened database connection
- */
- public function close()
- {
- if (!$this->link->close())
- {
- // error!
- trigger_error('Failed to close database.', E_USER_ERROR);
- }
- return true;
- }
- /**
- * Gets the number of affected rows in a previous MySQL operation
- */
- public function affected_rows()
- {
- return $this->link ? $this->link->affected_rows() : false;
- }
- /**
- * Fetch a result row as an associative array
- *
- * @param $result Returned by $this->query($sql)
- */
- public function fetchrow($result = false)
- {
- return $result ? $result->fetch_assoc() : $this->link->fetch_assoc();
- }
- /**
- * Frees the memory associated with a result
- *
- * @param $result Returned by $this->query($sql)
- */
- public function freeresult($result = false)
- {
- return $result ? $result->free() : $this->result->free();
- }
- /**
- * Returns the auto generated id used in the last query
- */
- public function insert_id()
- {
- return $this->link ? $this->link->insert_id() : false;
- }
- /**
- * Gets the number of rows in a result
- *
- * @param $result Returned by $this->query($sql)
- */
- public function num_rows($result = false)
- {
- //$result->num_rows()
- return $result ? mysqli_num_rows($result) : $this->result->num_rows();
- }
- /**
- * Performs a query on the database
- *
- * @param $sql The query string
- */
- public function query($sql)
- {
- if (!$this->result = $this->link->query($sql))
- {
- // error!
- trigger_error('<strong>Query error:</strong><br />' . mysqli_error($this->link) . '<br />File: ' . __FILE__ . '<br />Line: ' . __LINE__, E_USER_ERROR);
- }
- return $this->result;
- }
- /**
- * Escapes special characters in a string for use in a SQL statement, taking into account the current charset of the connection
- *
- * @param string $str The string to be escaped.
- * @return string Returns an escaped string.
- */
- public function escape_string($str)
- {
- // Magic Quotes escapes all incoming data to the PHP script.
- if (ini_get('magic_quotes_gpc'))
- {
- // To avoid escaping the string twice, we use stripslashes
- $str = stripslashes($str);
- }
- return $this->link->real_escape_string($str);
- }
- public function error($str)
- {
- return $this->error = $str;
- }
- }
- ?>
Add Comment
Please, Sign In to add comment