Welcome Guest, Not a member yet? Register   Sign In
Db query Ci3 to Ci4
#1

I not winning with simple tasks in CodeIgniter 4, instead of explaining the many errors and things ive tried, i thought i would try a different approach.
Can you please tell me how to write the following in Ci4
Code:
function login($username,$password)
{
$this->db->where('email', $username);
$this->db->where('password', $password);
$this->db->limit(1);
$row = $this->db->get('admin')->row_array();
var_dump($row);
}

Ive read the following and none seem to help me:
https://forum.codeigniter.com/thread-76383.html
https://codeigniter.com/user_guide/datab...cting-data
https://codeigniter.com/user_guide/model...l-creation

Its either the database connection is not available or in the following case:
Code:
<?php

namespace App\Models\Admin;

use CodeIgniter\Model;

class AccessModel extends Model
{
  protected $DBGroup = 'admin';
 
  // This function does the log in and stores the admin info from to DB to a session array
  function login($username,$password)
  {

    $this->where('email', $username);
    $this->where('password', $password);
    $sqlQuery  = $this->get();
    $row = $sqlQuery->getResult();
    var_dump($row);die();
  }

InvalidArgumentException

admin is not a valid database connection group.
Reply
#2

If it says admin is not a valid connection group, then it is what it is. Double check you db config.

Here is an example of what you can do for your login() function in your model:
PHP Code:
function login($username,$password)
{
    return $this->where([
        'email' => $username,
        'password' => $password,
    ])->first();

CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

Thank you. ill try this
however, admin is a column in my database and not the of my db so i would have anything to do with the db config file... right ?
I was just going through the recipe tutorial on your website Smile
Reply
#4

(10-16-2021, 09:24 AM)CodingInCodeigniter Wrote: however, admin is a column in my database and not the of my db so i would have anything to do with the db config file... right ?
$DBGroup is only used if you want your model to connect to a different database than the default config. Otherwise, you have nothing special to do and it will automatically connect to the default db.
See these 2 pages for more info :
DB Config: https://codeigniter.com/user_guide/datab...ation.html
Model: https://codeigniter.com/user_guide/model...e-database

If admin is a column then you can add it to the select. Otherwise it will select *.
PHP Code:
$this->select('admin')
     ->where([
         'email' => $username,
         'password' => $password,
     ])->first(); 


(10-16-2021, 09:24 AM)CodingInCodeigniter Wrote: I was just going through the recipe tutorial on your website Smile
Cool. Let me know if you have any question.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB