0

dbconnect.php

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

dao.php

include 'dbconnect.php';
class dao extends dbconnect
{
    private $conn;
    function __construct()
    {
        $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;
    }
}

controller.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';
    }
}

I got an error

  • Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /opt/lampp/htdocs/ankit_demo/dao.php on line 13

  • Warning: mysqli_error() expects parameter 1 to be mysqli, null given in /opt/lampp/htdocs/ankit_demo/dao.php on line 13

Please help me to solve this.

Shah Ankit
  • 119
  • 2
  • 4
  • 16

4 Answers4

4

Try this out, there was issues with if condition as well as the where condition. and we can't echo a object or can't convert object to string.

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");
    }
}
?>
Yogesh Koli
  • 95
  • 1
  • 7
-1

Change name of your constructor from __dao() to __construct().

Replace your line 6-th line of code by:

$this->conn = $dbcon->connect();
Taras
  • 572
  • 3
  • 15
  • i m replacing this but then also gives me same warning. – Shah Ankit May 03 '16 at 13:21
  • 1
    1. Add additional check: if (!$this->conn) throw new Exception('Can`t connect to database.'); after $this->conn = $dbcon->connect(); 2. How and where do you create new dao instance? – Taras May 03 '16 at 13:24
-1

try this :

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;
        }
    }
Ranjit Shinde
  • 1,121
  • 1
  • 7
  • 22
-1

Connection file:

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

dao file:

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;
    }
}

But I think it would be better to use PDO or much better a ORM system like laravel eloquent.

Andre
  • 385
  • 1
  • 13
  • ok it works now but give me another error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 – Shah Ankit May 03 '16 at 13:31
  • What are your params for $table, $where, $other? – Andre May 03 '16 at 13:33
  • 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(); header("location:index.php"); } else{ $_SESSION['error'] = 'Invalid Username Or Password'; } } – Shah Ankit May 03 '16 at 13:34
  • it holds the name of the table where variable contains the where condition and other holds an other condition – Shah Ankit May 03 '16 at 13:37
  • puh,... ehm.. first you should not use `extract` ... and you don't need it there because you use $_POST[...] the right way... next you sould always check the user inputs BEFORE you insert it direct in a sql statement. I think the error is there because you forget the " ; " at the end and the "WHERE" keyword in the sql statement. – Andre May 03 '16 at 13:45
  • And you dont need to check `if $result['email_id'] == $username` and the password because your sql statement only selects rows where the email_id is the username... you should check if you get more then zero rows.. or better exact one row if your have unique emails in your table – Andre May 03 '16 at 13:49