Hallo Allemaal!

Er zijn al menig classes voor MySQL, maar het schijnt een goede oefening te zijn!

Dus bij deze! Gaat dit goed zo ?

<?php
error_reporting(E_ALL);
class dbConnection
{
	// Basic Propertys
	protected $host = 'localhost';
	protected $user = 'laurens';
	protected $pass = 'landstede110';
	protected $db = 'test_db';
	
	// Changeable propertys
	protected $link_ID = 0;
	protected $select_DB = 0;
	protected $do_query = 0;
	
	function __construct()
	{
		$this->link_ID = mysql_connect($this->host, $this->user, $this->pass);
		if (!$this->link_ID)
		{
			return $this->trigger_db_error(mysql_error());
		}
		if ($this->link_ID)
		{
			$this->select_DB = mysql_select_db($this->db);
			if (!$this->select_DB)
			{
				return $this->trigger_db_error(mysql_error());
			}
		}
	}
	
	function do_query($query)
	{
		$this->do_query = mysql_query($query);
		if (!$this->do_query)
		{
			return $this->trigger_db_error(mysql_error());
		}
		
		while($row = mysql_fetch_array($this->do_query))
		{
			$Data[] = $row;
		}
		
		return $Data;
	}
	
	function trigger_db_error($error)
	{
		return sprintf('%s', $error);
	}
}

$Connection = new dbConnection();
print_r($Connection->do_query("SELECT * FROM persons"));
?>
Paar dingetjes:
-Wat als je met meerdere db's wil verbinden? Kan nu alleen maar met eentje.
-Waar slaat die sprintf op bij je trigger_error?
-Waarom geen exceptions?
-Waarom alleen mysql_fetch_array, en dan geen mode meegeven?

Zo zou ik het doen, en dan waarschijnlijk nog met losse methodes (zodat je kan kiezen welk fetch-type je gebruikt, in losse methodes), want stel nu dat je een UPDATE doet. Fetchen heeft dan totaal geen zin...

<?php
ini_set("display_errors",1);
error_reporting(E_ALL);

class dbConnection
{
const DEFAULT_HOST = 'localhost';
const DEFAULT_USER = 'laurens';
const DEFAULT_PASS = 'landstede110';
const DEFAULT_DB = 'test_db';

protected $connection;
protected $last_result;
protected $last_data;

function __construct($host=null,$user=null,$pass=null,$db=null)
{
$this->host=(empty($host)?DEFAULT_HOST:$host);
$this->user=(empty($user)?DEFAULT_USER:$user);
$this->pass=(empty($pass)?DEFAULT_PASS:$pass);
$this->db=(empty($db)?DEFAULT_DB:$db);

$this->link_ID = mysql_connect($this->host, $this->user, $this->pass);
if (!$this->link_ID)
{
throw new dbConnectionException("Could not connect to database, error: " . mysql_error());
return;
}
if ( ! mysql_select_db($this->db) )
{
throw new dbConnectionException("Could not connect to database, error: " . mysql_error());
}
}

function do_query($query,$type=MYSQL_ASSOC)
{
$this->last_result = mysql_query($query);
if (!$this->last_result)
{
throw new dbConnectionException("Could not execute query, error: " . mysql_error());
return false;
}

$data=array();

while($row = mysql_fetch_array($this->do_query,$type))
{
$data[] = $row;
}

return $data;
}

/*function trigger_db_error($error)
{
throw new dbConnectionException($error);
}*/
}

class dbConnectionException extends Exception{}

try
{
$Connection = new dbConnection();
print_r($Connection->do_query("SELECT * FROM persons"));
}
catch(dbConnectionException $e)
{
echo $e->getMessage();
}
?>

Reageren