CodeIgniter Forums
Working with Database in CI4 (for New Members) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: Working with Database in CI4 (for New Members) (/showthread.php?tid=77629)



Working with Database in CI4 (for New Members) - nc03061981 - 09-25-2020

Connect to Database
Code:
- in Model
$db = $this->db;
- not in Model
$db = db_connect();

Process Query
Code:
- with Query
$sql  = "Your query here";
$query = $db->query($sql);
- with Builder
$builder = $db->table("Your table name here");
$query  = $builder->getWhere($arrWhere, $limit);
$query  = $builder->insert($arrInsert);
$query  = $builder->update($arrUpdate, $arrWhere);

Check Result with $db->query and $bulder->getWhere
Code:
if (count($query->getResult()) > 0) {
foreach ($query->getResult() as $row)
{

}
or
$row = $query->getRow();

// Yes have data
// Code here
}

... and read more in User Guide...
and any comment for add...