Welcome Guest, Not a member yet? Register   Sign In
Unable to connect to database.
#1
Exclamation 

HI,
I am having a problem creating a model and using it in my controller. I am receiving the following error when trying to do so.

Quote:APP\Models\loginModel::__construct(): Argument #1 must be of type Codeigniter\Database\ConnectionInterface,
Codeigniter\Database\MySQLI\Connection given.


Here is the code that I am using for my model and controller.

Code:
<?php 

namespace APP\Models;
use App\Database;
use CodeIniter\Database\ConnectionInterface;
use CodeIgniter\Database\MySQLi\Connection;

class loginModel
{
    protected  $db;

    public function __construct(ConnectionInterface &$db)
    {
        $this->db =& $db;
    }

    public function getloginUser()
    {
        $builder = $this->db->table("loginusers");

        $result = $builder->get()->getresults();
        return $result;
    }
}

////-----------------------Controller------------------------////
Code:
<?php

namespace App\Controllers;
use App\Models\LoginModel;
class Login extends BaseController
{
    public function index()
    {
        return view('login');
    }

    public function validateLogin()
    {
        $db = db_connect();
        $model = new LoginModel($db);

        $username = $this->request->getpost("username");
        $password = $this->request->getpost("password");

        $password = md5($password);

        echo "<pre>";
        print_r($model->getloginUser());
        echo "</pre>";
    }
}

Can you guys help me to fix the issue that I am facing?
Reply
#2

That is strange, because Codeigniter\Database\MySQLI\Connection implements Codeigniter\Database\ConnectionInterface.
Reply
#3

You Posted this:
PHP Code:
use CodeIniter\Database\ConnectionInterface

It has a typo and should be:
PHP Code:
use CodeIgniter\Database\ConnectionInterface

Typo in your post or in your code?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB