2016-05-03 9 views
-1

/* Verbindungsdatei */Wie Datenbank mit mvc in PHP verbinden

class dbconnect{ 
     public function connect(){ 
      $host = 'localhost'; 
      $user = 'root'; 
      $pass = ''; 
      $db = 'demo'; 
      $connection = mysqli_connect($host,$user,$pass,$db); 
      return $connection; 
     } 
    } 

/* dao Datei */

include 'dbconnect.php'; 
    class dao extends dbconnect{ 
     private $conn; 
     function __dao(){ 
      $dbcon = new dbconnect(); 
      $conn = $dbcon->connect(); 
     } 
     function select($table , $where='' , $other=''){ 
      if(!$where = ''){ 
       $where = 'where' . $where; 
      } 
      $sele = mysqli_query($this->conn,"SELECT * FROM $table $where $other") or die(mysqli_error($this->conn)); 
      echo $sele; 
      return $sele; 
     } 
    } 
/* controler file */ 

include 'dao.php'; 

$d = new dao(); 



if(isset($_POST['btn_login'])){ 
    extract($_POST); 
    $username = $_POST['user_name']; 
    $pswd = $_POST['pswd']; 

    $sel = $d->select("users" , "email_id = '" . $username . "'AND password='" . $pswd . "'") or die('error from here'); 
    $result = mysqli_fetch_array($sel) ; 

    if($result['email_id'] == $username && $result['password'] == $pswd){ 
     SESSION_START(); 
     $_SESSION['user_name'] = $result['email_id']; 
     $_SESSION['message'] = 'Invalid Username Or Password'; 
     header("location:index.php"); 
    } 
    else{ 
     $_SESSION['error'] = 'Invalid Username Or Password'; 
     // header("Location:login.php"); 
    } 
} 

Ich habe einen Fehler Warnung: mysqli_query() erwartet Parameter 1 ist mysqli, null gegeben in /opt/lampp/htdocs/ankit_demo/dao.php on line 13

Warnung: mysqli_error() erwartet Parameter 1 als mysqli, null in/opt/lampp/htdocs/ankit_demo /dao.php auf l in 13

wie löst man das? Bitte helfen Sie mir, dies zu lösen.

+0

'$ this-> conn' ==' $ conn' – Rizier123

+0

Hauptproblem ist, dass Sie in Ihrem Konstruktor keine Klasseneigenschaft '$ conn' erstellen, sondern nur eine lokale Variable. Außerdem sollten Sie '__construct()' verwenden, nicht php4-style Konstruktoren (eigentlich eine seltsame Mischung) –

+0

dann wie kann ich das lösen? Bitte sagen Sie mir, ich bin ein neuer Entwickler in PHP –

Antwort

3

Probieren Sie dies aus, gab es Probleme mit, wenn Bedingung sowie die wo Bedingung. und wir können ein Objekt nicht wiedergeben oder das Objekt nicht in eine Zeichenfolge konvertieren.

dbconnect.php:

<?php 
class dbconnect{ 
    public function connect(){ 
     $host = 'localhost'; 
     $user = 'root'; 
     $pass = ''; 
     $db = 'demo'; 
     $connection = mysqli_connect($host,$user,$pass,$db); 
     return $connection; 
    } 
} 

dao.php:

<?php 
include 'dbconnect.php'; 
class dao extends dbconnect { 
    private $conn; 
    public function __construct() { 
     $dbcon = new parent(); 
     // this is not needed in your case 
     // you can use $this->conn = $this->connect(); without calling parent() 
     $this->conn = $dbcon->connect(); 
    } 

    public function select($table , $where='' , $other=''){ 
     if($where != ''){ // condition was wrong 
     $where = 'where ' . $where; // Added space 
     } 
     $sql = "SELECT * FROM ".$table." " .$where. " " .$other; 
     $sele = mysqli_query($this->conn, $sql) or die(mysqli_error($this->conn)); 
     // echo $sele; // don't use echo statement because - Object of class mysqli_result could not be converted to string 
     return $sele; 
    } 
    } 
?> 

controller.php:

<?php 
include 'dao.php'; 

$d = new dao(); 

if(isset($_POST['btn_login'])){ 
    extract($_POST); 
    $username = $_POST['user_name']; 
    $pswd = $_POST['pswd']; 

    $sel = $d->select("users" , "email_id = '" . $username . "' AND password='" . $pswd . "'") or die('error from here'); 
    $result = mysqli_fetch_array($sel) ; 

    if($result['email_id'] == $username && $result['password'] == $pswd){ 
     SESSION_START(); 
     $_SESSION['user_name'] = $result['email_id']; 
     $_SESSION['message'] = 'Invalid Username Or Password'; 
     header("location:index.php"); 
    } 
    else{ 
     $_SESSION['error'] = 'Invalid Username Or Password'; 
     // header("Location:login.php"); 
    } 
} 
?> 
0

Ändern Sie den Namen Ihres Konstruktors von __dao() in __construct().

Ihre Linie 6-ten Codezeile ersetzen durch:

$this->conn = $dbcon->connect(); 
+0

Ich ersetze dies aber gibt mir auch die gleiche Warnung. –

+1

1. Zusätzliche Überprüfung hinzufügen: Wenn (! $ This-> conn) neue Exception werfen ('Can 't connect to database.'); nach $ this-> conn = $ dbcon-> connect(); 2. Wie und wo erstellen Sie eine neue Dao-Instanz? – Taras

0

versuchen, dieses:

include 'dbconnect.php'; 
    class dao extends dbconnect{ 
     private $conn; 
     function __construct(){ 
      $dbcon = new dbconnect(); 
      $this->conn = $dbcon->connect(); 
     } 
     function select($table , $where='' , $other=''){ 
      if(!$where = ''){ 
       $where = 'where' . $where; 
      } 
      $sql = "SELECT * FROM ".$table." " .$where. " " .$other"; 
      $sele = mysqli_query($this->conn, $sql) or die(mysqli_error($this->conn)); 
      echo $sele; 
      return $sele; 
     } 
    } 
0

Verbindungsdatei:

class dbconnect{ 
    public function connect(){ 
     $host = 'localhost'; 
     $user = 'root'; 
     $pass = ''; 
     $db = 'demo'; 
     $connection = mysqli_connect($host,$user,$pass,$db); 
     return $connection; 
    } 
} 

dao-Datei:

include 'dbconnect.php'; 
class dao extends dbconnect { 
    private $conn; 
    public function __construct() { 
     $dbcon = new parent(); // this is not needed in your case 
     // you can use $this->conn = $this->connect(); without calling parent() 
     $this->conn = $dbcon->connect(); 
    } 
    public function select($table , $where='' , $other=''){ 
     if(!$where = ''){ 
      $where = 'where' . $where; 
     } 
     $sele = mysqli_query($this->conn,"SELECT * FROM $table $where $other") or die(mysqli_error($this->conn)); 
     echo $sele; 
     return $sele; 
    } 
} 

Aber ich denke, es wäre besser, PDO oder viel besser ein ORM-System wie Laravel eloquent zu verwenden.

+0

ok es funktioniert jetzt, aber geben Sie mir einen anderen Fehler Sie haben einen Fehler in Ihrer SQL-Syntax; Überprüfen Sie das Handbuch, das Ihrer MySQL-Server-Version für die richtige Syntax in der Nähe von '' in Zeile 1 entspricht –

+0

Was sind Ihre Parameter für $ Tabelle, $ wo, $ andere? – Andre

+0

enthalten 'dao.php '; \t $ d = neuer dao(); \t if (isset ($ _POST ['btn_login'])) { \t \t extrahieren ($ _ POST); \t \t $ username = $ _POST ['user_name']; \t \t $ pswd = $ _POST ['pswd']; \t \t $ sel = $ d-> auswählen ("users", "email_id = '". $ Username.' 'AND password =' ​​". $ Pswd." '") Oder sterben (' error from here ') ; \t \t $ result = mysqli_fetch_array ($ sel); \t \t \t \t if ($ result [ 'email_id'] == $ username && $ result [ 'password'] == $ pswd) { \t \t \t session_start(); \t \t \t header ("location: index.php"); \t \t} \t \t else { \t \t \t $ _SESSION [ 'error'] = 'Benutzername oder Passwort ungültig'; \t \t} \t} –