Sådan ser min kode ud indtil videre:
- <?php
-
- class DatabaseMySQL
- {
- private $activeConnection = 0;
- private $connections = array();
- private $lastQuery;
- private $messsages = array();
-
- public function __construct($hostname, $username, $password, $database)
- {
- $this->newConnection($hostname, $username, $password, $database);
- }
-
- public function __destruct()
- {
- $countConnections = sizeof($this->connections);
-
- for($i = 0; $i <= $countConnections; $i++)
- {
- @mysql_close($this->connections[$i]);
- }
- }
-
- public function closeConnection()
- {
- @mysql_close($this->connections[$this->activeConnection]);
- }
-
- public function executeQuery($queryString)
- {
- try
- {
- if(!$query = @mysql_query($queryString, $this->connections[$this->activeConnection])
- {
- throw new Exception("The query couldn't be executed. Try again later.");
- }
- else
- {
- $this->lastQuery = $query;
- }
- }
- catch(Exception $e)
- {
- $this->newMsg($e->getMessage());
- }
- }
-
- public function newConnection($hostname, $username, $password, $database)
- {
- try
- {
- $this->connections[] = @mysql_connect($hostname, $username, $password);
- $thisConnection = $this->connections[count($this->connections) - 1];
-
- if($thisConnection)
- {
- $selectDatabase = @mysql_select_db($database, $thisConnection);
-
- if(!$selectDatabase)
- {
- throw new Exception("There couldn't be established a connection to the database.");
- }
- }
- else
- {
- throw new Exception("There couldn't be established a connection to the DBMS.");
- }
-
- $connection_id = count($this->connections) - 1;
-
- return $connection_id;
- }
- catch(Exception $e)
- {
- $this->newMsg($e->getMessage());
- }
- }
-
- public function secureString($input)
- {
- if(get_magic_quotes_gpc())
- {
- $input = stripslashes($input);
- }
- return @mysql_real_escape_string($input);
- }
-
- public function setActiveConnection($new)
- {
- $this->activeConnection = $new;
- }
-
- private function newMsg($msg)
- {
- $this->messsages[] = $msg;
- }
- }
-
- ?>
Spørgsmålet er bare om try/catch skal bruges uden for min klasse, hvis den skal benyttes "rigtigt" eller er det lige gyldigt?
Indlæg senest redigeret d. 17.09.2009 21:44 af Bruger #6559