Pergunta

I have two files, one file has a class and a method to connect to the database, the second file has a class to login a user and of course I need to call the class and method in the second file to allow the login.

The problem I am having is when i Call the file that contains the class and method to connect is undefined.

File 1 "Connection Details"

    <?php

class dbConnect{

    protected $host = 'localhost';
    protected $user = 'root';
    protected $pass = 'root';
    protected $db = 'bolt_mail';

    public function connect(){
        $con = mysqli_connect($this->host,$this->user,$this->pass,$this->db) or die('Cannot Connect to DB');
        return $con;
    }

}

File 2 "Login"

class login{

public function con(){
    include ('connect.php');

    $db = new dbConnect();
    $db->connect();

     return $db;
}

public function getLogin($email, $pass){



    $sql = "SELECT ott_email, ott_password ,app_password , user_status, account_type FROM tbl_user WHERE ott_email='$email' AND app_password='$pass'";
    $query = mysqli_query($this->con(), $sql);
    $count = mysqli_num_rows($query);

Errors:

    Warning: mysqli_query() expects parameter 1 to be mysqli, object given in /Applications/MAMP/htdocs/project_ott/php/login.php on line 38

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /Applications/MAMP/htdocs/project_ott/php/login.php on line 39

Can someone see what I am doing wrong...?

Foi útil?

Solução

Try this,

public function con(){
    include ('connect.php');

    $db = new dbConnect();
   $conn =  $db->connect();

     return $conn ;
}

You are trying to return the object of class dbConnect and not the return of function connect. Also your db connection design needs to be improved, have a look at singleton pattern

Outras dicas

Move this string

include ('connect.php');

to the top of Your second file, in other words, outside the class login. And this strings:

$db->connect();

return $db;

change to:

return $db->connect();

As shown in the first warning, mysqli_query() expects parameter 1 to be mysqli, not some other object(in your case it's your dbConnect object that is returned by con). Since you are returning the mysqli connection from your connect method, you should return that same connection in your con function to be used by mysqli_query(). Try this...

class login{

public function con(){
    include ('connect.php');

    $db = new dbConnect();
    return $db->connect();
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top