_create($url);
}
/**
* _create
*
* Creates the DB connection.
*
* @param $url The url with the database params. Example: pgsql://username:password@localhost/databasename
*
* @version 0.1
*/
function _create($url) {
$url = parse_url($url);
$dbuser = urldecode($url['user']);
// Test if database url has a password.
if (isset($url['pass'])) {
$dbpassword = urldecode($url['pass']);
} else {
$dbpassword = '';
}
$dbhost = urldecode($url['host']);
$dbname = urldecode($url['path']);
if (isset($url['port'])) {
$dbhost = $dbhost . ':' . $url['port'];
}
if ( defined('dbcharset') )
$this->charset = dbcharset;
$this->dbcon = @mysql_connect( $dbhost, $dbuser, $dbpassword);
if (!$this->dbcon){
pt_kill("There were problems with the database connection, please check the information of your pt_settings.php file.
- Make sure your username and your password are right.
- Make sure your hostname is right. The hostname i attempted to contact is
$dbhost
- Make sure your database server is running
");
return false;
}
if ( !empty($this->charset) && version_compare(mysql_get_server_info(), '4.1.0', '>=') )
@mysql_query("SET NAMES '$this->charset'", $this->dbcon);
@mysql_set_charset($this->charset);
$this->select_db($dbname);
}
/**
* select_db
*
* Selects the database to work with.
*
* @param $dbname The database name
*
* @version 0.1
*/
function select_db($dbname) {
if (!@mysql_select_db($dbname)){
pt_kill("I was unable to select de database so:
- Make sure this is your database name
$dbname
Your username and password are right so don’t modify them.");
}
}
/**
* db_query
*
* Executes a query.
*
* @param $query The query to execute
* @param $fetch The type of fetch wished. Example: db_query($query, array);
*
* @version 0.1
*/
function db_query($query, $fetch = "") {
$this->flushVars();
$this->execution = @mysql_query( $query, $this->dbcon);
if ( mysql_error($this->dbcon)){
//to handle mysql errors;
}
if ( preg_match("/^\\s*(insert|delete|update|replace) /i",$query) ) {
$this->affected_rows = mysql_affected_rows($this->dbcon);
} else {
switch ($fetch) {
case 'array':
$i=0;
while ($row = @mysql_fetch_array($this->execution, MYSQL_ASSOC)) {
$this->row[$i] = $row;
$i++;
}
break;
case 'row':
$this->row = @mysql_fetch_row($this->execution);
break;
case 'field':
$i=0;
$this->affected_fields = @mysql_num_fields($this->execution);
while ($i < $this->affected_fields) {
$this->col = @mysql_fetch_field($this->result, $i);
$i++;
}
break;
case 'alive':
return true;
default:
$i = 0;
while ( $row = @mysql_fetch_object($this->execution) ) {
$this->row[$i] = $row;
$i++;
}
$this->affected_rows = $i;
break;
}
}
}
/**
* flushvars
*
* Flushes the vars of the module.
*
* @version 0.1
*/
function flushVars() {
$this->query = null;
$this->error = null;
$this->result = null;
$this->execution = null;
}
/**
* is_alive
*
* Check if the db connection is alive.
*
* @return true Return tru if the connection is alive
*
* @version 0.1
*/
function is_alive() {
if (db_query("SELECT 1"))
return True
}
/**
* db_version
*
* Gets the version of the db server.
*
* @return version The database version.
*
* @version 0.1
*/
function db_version() {
list($version) = explode('-', mysql_get_server_info());
return $version;
}
}
?>