Problemer med klasse
Jeg har lavet følgende klasse til at oprette forbindelse til mysql:<?php
class Mysql {
private $connection;
public static $instance;
public function __construct($host, $username, $password, $database, $newLink = false) {
$this->connection = mysql_connect($host, $username, $password, $newLink);
if (!$this->connection){
throw new Exception("Could not connect to the server...");
}
if(!mysql_select_db($database, $this->connection)){
throw new Exception("Could not connect to the database...");
}
}
public static function instance(){
if(!isset(Mysql::$instance)){
Mysql::$instance = new Mysql(MYSQL_HOSTNAME, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE);
}
return Mysql::$instance;
}
public function selectDataObject($query){
return mysql_fetch_object(mysql_query($query, $this->connection));
}
public function selectDataArray($query){
return mysql_fetch_assoc(mysql_query($query, $this->connection));
}
public function __destruct() {
if(!mysql_close($this->connection)){
throw new Exception("Could not close the connection to the server...");
}
}
}
?>
Nu vil jeg gerne udskrive det:
try {
$Database = Mysql::instance();
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
while($Data = $Database->selectDataObject("SELECT * FROM categories ORDER BY Name"))
{
echo $Data->ID;
}
Den finder også noget data fra databasen men udskriver bare det samme uendeligt, altså løkken stopper ikke.. :S