eine verbindung wird über eine "DSN" aufgebaut, das ist eine Url in der form: mysql://username
![Stick Out Tongue](https://www.php-resource.de/forum/core/images/smilies/tongue.gif)
folgendes kommt dabei raus, ich denke das ist praktischer.
könnt ihr gerne benutzen oder euch was daran abschauen.
PHP-Code:
<?php
/**
* USAGE:
* $dsn = "mysql://username:password@host/database_name";
* if ($sql = new sql($dsn)) {
* connection is ok.
* more commands can follow here
* } else {
* connection not established
* }
*
**/
/**
* sql
* basic sql functions
*/
class sql {
/**
* @var resource
* resource id of mysql connection
* @access private
**/
var $conn;
/**
* @var resource
* resource id of last executed query
* @access public
**/
var $result;
/**
* @var string
* mysql error information
* @access public
**/
var $error;
/**
* sql::sql()
* constructor
*
* checks and parses string $dsn to mysql connection data
* connects to database and selects table
* returns TRUE if connection is established and database selected
* returns FALSE if an error occured
*
* @param string $dsn mysql connection url
* @return boolean
**/
function sql($dsn)
{
// check if dsn is correct
if (!preg_match("/mysql:\/\/([\w]+):([\w]+)@([\w]+)\/([\w]+)/s", $dsn, $arr)) {
$this->error = "DSN is not correct";
return FALSE;
}
// parse mysql data
$dsn = parse_url($dsn);
// remove slashes from databasename
$dsn['path'] = str_replace("/", "", $dsn['path']);
// connect to mysql server
if (!$this->db_connect($dsn['host'], $dsn['user'], $dsn['pass'])) {
return FALSE;
}
// select database
if (!$this->db_select($dsn['path'])) {
return FALSE;
}
return TRUE;
}
/**
* sql::db_connect()
* @access public
*
* connects to a mysql server.
*
*
* @param $host hostname to connect to
* @param $user username to connect with
* @param $pass password to connect with
* @return boolean connection established
**/
function db_connect($host, $user, $pass)
{
$this->conn = mysql_connect($host, $user, $pass);
// check for errors
if ($this->is_error()) {
return FALSE;
}
return TRUE;
}
/**
* sql::db_select()
* @access public
*
* selects database
*
* @param $db
* @return boolean
**/
function db_select($db)
{
mysql_select_db($db);
// check for errors
if ($this->is_error()) {
return FALSE;
}
return TRUE;
}
/**
* sql::is_error()
*
* checks if the last mysql operation failed and error information is available.
* sets $error to use in following debugging operations.
*
* @return boolean
* @access public
**/
function is_error()
{
// check if there was an error
if (mysql_errno()) {
// set $error for debugging
$this->error = mysql_error();
return TRUE;
}
// no error occured
return FALSE;
}
/**
* sql::query()
*
* performs a mysql_query using param $query
* - returns a resource if the query was a SELECT and one row or more could be selected.
* - returns TRUE if the query was UPDATE, DELETE or similiar and succeeded.
* - returns FALSE if the query failed or no rows were selected.
*
* @param string $query
* query to perform
*
* @return resource|boolean
* @access public
**/
function query($query)
{
$this->result = mysql_query($query);
// check for error
if ($this->is_error()) {
return FALSE;
}
if (mysql_affected_rows() >= 0) {
// UPDATE/DELETE Query was successfull
return TRUE;
}
if (mysql_num_rows($this->result) > 0) {
// one or more rows could be selected, resource can be used
return $this->result;
}
return FALSE;
}
/**
* sql::fetch()
*
* returns an array with the results or FALSE
* use print_r() to view the array structure
*
* @param $query
* query to perform
*
* @return array|boolean
* @access public
**/
function fetch($query)
{
// execute query and check for errors or NULL-results
if (!$this->query($query)) {
return FALSE;
}
// make an array containing the results
$return = array();
while ($row = mysql_fetch_assoc($this->result)) {
$return[] = $row;
}
return $return;
}
/**
* sql::insert()
*
* inserts values into a given table.
*
* @param string $table
* name of table to insert into
*
* @param array $insert
* array (field => value)
*
* @return boolean
* @access public
**/
function insert($table, $insert)
{
// form query out of array
$fields = "(" . implode(",",array_flip($insert)) . ")";
$values = "('" . implode("','",$insert) . "')";
$query = "INSERT INTO " . $table . " " . $fields . " VALUES " . $values . ";";
// execute query and check for errors
if ($this->query($query)) {
return TRUE;
}
return FALSE;
}
/**
* sql::free()
* @access public
*
* free's memory after mysql usage
*
* @return boolean
**/
function free()
{
if(mysql_free_result($this->result)) {
return TRUE;
} else {
return FALSE;
}
}
}
?>
Einen Kommentar schreiben: