01-29-2009, 12:29 AM
|
#73 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
php Code:
<?php class DBmysql { private $host; private $user; private $pass; private $data; private $con; private $query_sql; private $query_result; private $query_fetch = array(); public function __destruct() { if(isset($this-> con)) { $this-> disconnect(); } } /** * Basic description: * Sets the connection details * * Conditions: * - * * Detailed description: * - * * @param array $array * @return $this */ public function setHandler ($host, $user, $pass, $data) { $this-> host = $host; $this-> user = $user; $this-> pass = $pass; $this-> data = $data; return $this; } /** * Basic description: * Connects to the database using the connection details set by setHandler function * * Conditions: * You have to set the connection-values with setHandler before using this function * * Detailed description: * - * * @return $this */ public function connect () { $this-> con = mysql_connect($this-> host, $this-> user, $this-> pass); unset($this-> pass); if($this-> con) { return $this; } return false; } /** * Basic description: * Selects the database using the connection details set by setHandler function * * Conditions: * The mysql has to be connected * * Detailed description: * - * * @return $this */ public function select () { if(mysql_select_db($this-> data, $this-> con)) { return $this; } return false; } /** * Basic description: * Disconnects the current connection * * Conditions: * The mysql has to be connected * * Detailed description: * - * * @return true or false */ public function disconnect () { if(mysql_close($this-> con)) { return true; } return false; } /** * Basic description: * Loads a query, specified in the parameter * * Condition: * - * * Detailed description: * - * * @param string $query * @return $this */ public function loadQuery ($query) { $this-> query_sql = $query; return $this; } /** * Basic description: * Executes a query * * Conditions: * You have to load a query before using this function * * Detailed description: * If you haven't loaded a query, you can set the first parameter to the sql statement and it will load the query automaticly * * @param string(optional) $sql * @return $this */ public function exeQuery ($sql = NULL) { if($sql == NULL) { if(isset($this-> query_sql)) { $this-> query_result = mysql_query($this-> query_sql, $this-> con); if($this-> query_result) { return $this; } return false; } return false; } $this-> loadQuery($sql)-> exeQuery(); } /** * Basic description: * Gets the results from the query * * Conditions: * You have to execute a query before you can use this function * * Detailed description: * If you want a specific row, fill in the first parameter with the number of the row. * If you just want a specific column, fill in the second parameter with the name of the field * aswell as the first parameter with the number of the row * * @param int(optional) $row * @param string(optional) $name * @return $this->query_result */ public function getQueryResult ($row = NULL, $name = NULL) { if(isset($this-> query_result)) { if($row != NULL && is_numeric($row)) { if($name != NULL) { return mysql_result($this-> query_result, $row, $name); } return mysql_result($this-> query_result, $row); } return $this-> query_result; } return false; } /** * Basic description: * Loads and executes a fetch array * * Conditions: * You have to execute a query before you can use this function * * Detailed description: * If you want it to fetch the array as ASSOC, you need to specify that by setting the first parameter to true. * If you haven't executed an query, you can set the second parameter to the sql statement and it will execute the query automaticly * * @param boolean(optional) $assoc * @param string(optional) $sql * @return $this */ function loadFetch ($assoc = false, $sql = NULL) { unset($this-> query_fetch); if(is_null($sql) && isset($this-> query_result)) { $query_result = $this-> query_result; } else { $query_result = $this-> exeQuery($sql)-> getQueryResult(); } if($assoc == true) { $fetchType = MYSQL_ASSOC; } else { $fetchType = MYSQL_NUM; } if(! empty($query_result)) { while($fetch = mysql_fetch_array($query_result, $fetchType)) { $this-> query_fetch[] = $fetch; } if(is_array($this-> query_fetch)) { return $this; } return false; } return false; } /** * Basic description: * Gets the results from the loaded fetch * * Conditions: * You have to load the fetch by using loadFetch before using this function * * Detailed description: * If you haven't loaded the fetch, you can autoload it by setting the first parameter to true. * If autoload is set to true, you can specify if you want to fetch the array as assoc by setting the second parameter * to true or false. * * If autoload is set to true, you can specify the sql statement if you haven't already executed the query like this (example): * $fetch = $db->exeQuery($sql)->getFetch(true, true); * * You can, as said, skip the exeQuery function by specifying the sql parameter in this function like this (example): * $fetch = $db->getFetch(true, true, $sql); * * @param boolean(optional) $autoload * @param boolean(optional) $assoc * @param string(optional) $sql * @return $this->query_fetch */ public function getFetch ($autoload = false, $assoc = false, $sql = NULL) { if($autoload == false) { if(isset($this-> query_fetch) && is_array($this-> query_fetch)) { return $this-> query_fetch; } return false; } $this-> loadFetch($assoc, $sql)-> getFetch(); } /** * Basic description: * Gets the number of rows * * Conditions: * A query must be specified, either in the parameter, or a previously executed query * * Detailed description: * You can get the number of rows the query returned in 2 ways, either by executing a query before using this function, like this (example): * $numrows = $db->exeQuery($sql)->getRows(); * * Or by setting the parameter to the sql statement, like this (example): * $numrows = $db->getRows($sql); * * @param string(optional) $query * @return number of rows based on the query */ public function getRows ($query = NULL) { if(is_null($query)) { if(isset($this-> query_result)) { return mysql_num_rows($this-> query_result); } return false; } $this-> loadQuery($query)-> exeQuery()-> getRows(); } /** * Basic description: * Secures a string against possible mysql injections * * Conditions: * The parameter must be set * * Detailed description: * If the parameter is set to an int, it will return just the integer without cleaning it. * If the parameter is set to a string, it will clean the string from mysql injection before returning it * * @param string $value * @return $secured */ public function secure ($value) { if(get_magic_quotes_gpc()) { $value = stripslashes($value); } else { $value = $value; } if(is_numeric($value)) { $secured = $value; } else { $secured = mysql_real_escape_string($value); } return $secured; } }?>
Better?
__________________
|
|
|
|