2016-07-30 22 views
-1

ich lerne oop, ich habe diese Schleppfehler (mysqli_query() erwartet Parameter 1 zu sein mysqli, null, gegeben in C: \ wamp \ www \ oop2 \ model \ login.php in Zeile 38,: mysqli_num_rows() erwartet Parameter 1 als mysqli_result, null in C: \ wamp \ www \ oop2 \ model \ login. php in Zeile 40) und ich weiß nicht, wie der richtige Weg zu meinem db in oop und mysqli verbinden.Ich kenne nicht den richtigen Weg zu meinem db in meinem Login-Modell zu verbinden, ich benutze (oop - mysqli)

Datenbank verbinden Datenbank

class Database 
{ 
private $host; 
private $user; 
private $password; 
private $database; 

function __construct($filename) 
{ 
    if(is_file($filename)) include $filename; 
    else throw new Exception("Error!"); 

    $this->host  = $host; 
    $this->user  = $user; 
    $this->password = $password; 
    $this->database = $database; 

    $this->connect(); 
} 

public function connect() 
{ 
    // connect to the server 
    if(!mysqli_connect($this->host,$this->user, $this->password,$this->database)) 
     throw new Exception("Error: not connected to the server.");   

} 

function close() 
{ 
    mysql_close(); 
} 
} 

Login Modell verbinden

class Login{ 

private $username; 
private $password; 
private $cxn; 

public function __construct($username,$password) 
{ 
    $this->setData($username,$password); 
    $this->connectToDb(); 
    $this->getData(); 
} 

private function setData($username,$password) 
{ 
    $this->username = $username; 
    $this->password = $password; 
} 
private function connectToDb() 
{ 
    include '../model/database.php'; 
    $config = "../model/config.php"; 
    $this->cxn = new Database($config); 
    $this->cxn->connect(); 

} 

public function getData() 
{ 
     $query = "SELECT * FROM admin 
       WHERE 
       'username' = '$this->username' 
       AND 
       'password' = '$this->password' 
        "; 
     $sql = mysqli_query($this->cxn->connect(),$query); 

     if (mysqli_num_rows($sql) > 0) 
     { 
      return TRUE; 
     } 

     else 
     { 
      throw new Exception("Error Processing Request"); 

     } 
} 
public function close() 
{ 
    $this->cxn->close(); 
} 

} 

Antwort

0

mysqli_connect gibt ein Link-Objekt für die Verbindung zum MySQL-Server

class Login{ 

private $username; 
private $password; 
private $cxn; 

public function __construct($username,$password) 
{ 
    $this->setData($username,$password); 
    $this->connectToDb(); 
    $this->getData(); 
} 

private function setData($username,$password) 
{ 
    $this->username = $username; 
    $this->password = $password; 
} 
private function connectToDb() 
{ 
    include '../model/database.php'; 
    $config = "../model/config.php"; 
    $this->cxn = new Database($config, $host, $user, $password, $database); 
    $this->cxn->connect(); 

} 

public function getData() 
{ 
     $query = "SELECT * FROM admin 
       WHERE 
       'username' = '$this->username' 
       AND 
       'password' = '$this->password' 
        "; 
     $sql = mysqli_query($this->cxn->connect(),$query); 

     if (mysqli_num_rows($sql) > 0) 
     { 
      return TRUE; 
     } 

     else 
     { 
      throw new Exception("Error Processing Request"); 

     } 
} 
public function close() 
{ 
    $this->cxn->close(); 
} 

} 

Und Ihre Datenbank-Klasse ändern. In Ihrer Database Klasse müssen Sie den Rückgabewert von mysqli_connect sparen:

class Database { 
    private $host; 
    private $user; 
    private $password; 
    private $database; 
    private $connection; //Add this 

Dann:

public function connect() { 

    if(!$this->connection) { 
     // connect to the server 
     $this->connection = mysqli_connect($this->host,$this->user, $this->password,$this->database); 
     //Do some error checking here ! 
    } 
    return $this->connection;   

} 
+0

Nicht definierte Variable: Verbindung in C: \ wamp \ www \ oop2 \ model \ database.php in Zeile 26 ---- Kann nicht auf leere Eigenschaft in C: \ wamp \ www \ oop2 \ model \ database zugreifen .php auf Linie 26 – ziko310

+0

Ja, mein Schlechter Sorry! habe nur die Antwort aktualisiert, ich habe vergessen, das '$' Zeichen zu entfernen! '$ this -> $ connection' zu' $ this-> connection' !!! –

+0

ok es ist jetzt gelöst, aber ich bekomme den Fehler der Ausnahme in Login-Modell – ziko310

0

Sie müssen $ host, $ user, $ password und $ Datenbank DataBase Klasse wie folgt übergeben. mit diesem

class Database 
{ 
private $host; 
private $user; 
private $password; 
private $database; 

function __construct($filename, $host, $user, $password, $database) 
{ 
    if(is_file($filename)) include $filename; 
    else throw new Exception("Error!"); 

    $this->host  = $host; 
    $this->user  = $user; 
    $this->password = $password; 
    $this->database = $database; 

    $this->connect(); 
} 

public function connect() 
{ 
    // connect to the server 
    if(!mysqli_connect($this->host,$this->user, $this->password,$this->database)) 
     throw new Exception("Error: not connected to the server.");   

} 

function close() 
{ 
    mysql_close(); 
} 
} 
+0

Er bekommt die Werte aus einer Konfigurationsdatei! Dies ist nicht das eigentliche Problem –