Sto cercando di utilizzare la seguente classe
codice:
<?php
class MySQL {
var $host;
var $user;
var $pass;
var $data;
var $link;
public function MySQL() { // Costruttore
$param = func_get_args();
foreach($param as $arg) {
if(!isset($arg) || strlen(trim($arg)) == 0)
die("Parametri di connessione non validi!");
}
}
public function setConfig($host, $user, $pass) {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
}
public function Connect() {
$this->link = mysql_connect($this->host, $this->user, $this->pass)
or die("Impossibile creare la connessione!");
if($this->link)
return $this->link;
else
die("Errore nella connessione al database");
}
public function SelectDB($db) {
if(!isset($db) || strlen(trim($db)) == 0)
die("Nome del database non valido!");
mysqli_select_db($this->link, $db) or die("Impossibile selezionare il database!");
}
public function SetCharset($charset) {
mysqli_query($this->link, "SET NAMES $charset") or die("Impossibile impostare il charset della connessione");
}
public function getCon() {
return $this->link;
}
}
?>
Però quando vado a fare "SelectDB()" o "SetCharset()" mi dice
Warning: mysqli_select_db() expects parameter 1 to be mysqli, resource given in C:\Programmi\Apache Group\Apache2\htdocs\WestSystems.it\includes\class .MySQL.php on line 44
Eppure prima dell'utilizzo di "$this->link" faccio
codice:
var_dump($this->link);
mi restituisce
resource(9) of type (mysql link)
Non capisco perchè non mi prenda correttamente l'argomento.
Il resto del codice è:
codice:
final class singleton {
private static $instances = array();
public static function getInstance($class_name) {
if(!class_exists($class_name)) {
trigger_error("La classe $class_name non esiste!", E_USER_ERROR);
}
$class_name = strtolower($class_name);
if(!array_key_exists($class_name, self::$instances))
{
self::$instances[$class_name] = new $class_name;
}
return self::$instances[$class_name];
}
}
codice:
$mysql = singleton::getInstance('MySQL');
$mysql->setConfig($db_host, $db_user, $db_pass);
$mysql->Connect();
$mysql->SelectDB($db_name);
$mysql->SetCharset($db_charset);
$con = $mysql->getCon();