Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @author profusion
- * @copyright 2011
- */
- class MySQL {
- /**
- * @name linkID
- * @var resource
- * @access private
- */
- private $linkID = null;
- /**
- * @name dbHost
- * @var string
- * @access private
- */
- private $dbHost;
- /**
- * @name dbUser
- * @var string
- * @access private
- */
- private $dbUser;
- /**
- * @name dbPass
- * @var string
- * @access private
- */
- private $dbPass;
- /**
- * @name dbName
- * @var string
- * @access private
- */
- private $dbName;
- /**
- * @name lastSQL
- * @var resource
- * @access private
- */
- private $lastSQL = null;
- /**
- * @name __construct
- * @param dbHost
- * @param dbUser
- * @param dbPass
- * @param dbName
- * @access public
- * @return void
- */
- public function __construct($dbHost, $dbUser, $dbPass, $dbName) {
- $this->dbHost = (!empty($dbHost) ? $dbHost : 'localhost');
- $this->dbUser = $dbUser;
- $this->dbPass = $dbPass;
- $this->dbName = $dbName;
- $this->connect();
- }
- /**
- * @name connect
- * @access private
- * @return void
- */
- private function connect() {
- if (false == $this->linkID = @mysql_connect($this->dbHost, $this->dbUser, $this->dbPass)) {
- $this->error('Unable to connect server "'.$this->dbHost.'" with username "'.$this->dbUser.'"!');
- } elseif (false == @mysql_select_db($this->dbName, $this->linkID)) {
- $this->error('Unable to connect database "'.$this->dbName.'"!');
- }
- }
- /**
- * @name sendQuery
- * @param query
- * @access public
- * @return mixed
- */
- public function sendQuery($query) {
- if (false == $queryResult = @mysql_query($query)) {
- $this->error('Unable to execute query "'.$query.'"!');
- } else {
- return $this->lastSQL = $queryResult;
- }
- }
- /**
- * @name fetch
- * @param query
- * @access public
- * @return mixed
- */
- public function fetch($query = '') {
- $query = (empty($query) ? $this->lastSQL : $query);
- $query = (!is_resource($query) ? $this->sendQuery($query) : $query);
- $array = @mysql_fetch_assoc($query);
- $this->freeResult($query);
- return $array;
- }
- /**
- * @name numRows
- * @param result
- * @access public
- * @return mixed
- */
- public function numRows($result = '') {
- return @mysql_num_rows((!empty($result) ? $result : $this->lastSQL));
- }
- /**
- * @name insert
- * @param data
- * @param table
- * @access public
- * @return mixed
- */
- public function insert($data, $table) {
- $cols = $keys = array();
- foreach ($data as $col => $key) {
- $key = (is_int($key) || is_numeric($key) ? (int)$key : $this->escapeString($key));
- $cols[] = '`'.$col.'`';
- $keys[] = '\''.$key.'\'';
- }
- // preparing sql
- $sql = 'INSERT INTO `'.$table.'` ';
- $sql .= '('.implode(', ', $cols).')';
- $sql .= ' VALUES ';
- $sql .= '('.implode(', ', $keys).');';
- return $this->sendQuery($sql);
- }
- /**
- * @name freeResult
- * @param result
- * @access public
- * @return mixed
- */
- public function freeResult($result) {
- @mysql_free_result($result);
- }
- /**
- * @name escapeString
- * @param string
- * @access public
- * @return string
- */
- public function escapeString($string) {
- return mysql_real_escape_string(addslashes($string), $this->linkID);
- }
- /**
- * @name error
- * @param text
- * @access private
- * @return void
- */
- private function error($text) {
- die('<h1>MySQL Class Error:</h1> '.$text.'<br /><hr />MySQL Error: '.mysql_error().'<br /> MySQL Errno: '.mysql_errno());
- }
- /**
- * @name __destruct
- * @access public
- * @return void
- */
- public function __destruct() {
- @mysql_close($this->linkID);
- }
- }
- // new object
- $MySQL = new MySQL('localhost', 'root', '', 'database');
- // insert
- $MySQL->insert(array(
- // col // value
- 'userName' => 'hallo123'
- ), 'users');
- // fetching a query
- $MySQL->sendQuery('SELECT `userName` FROM `users` WHERE `userName` = '.$this->escapeString($_GET['userName']).';');
- // rows
- $rows = $MySQL->numRows();
- // row
- $row = $MySQL->fetch();
- /*
- echo 'rows: '.$rows;
- echo 'username: '.$row['userName'];
- */
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement