CodeIgniter Forums
Sql in Model - Undefined variable: db - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Sql in Model - Undefined variable: db (/showthread.php?tid=78042)



Sql in Model - Undefined variable: db - henry_miaomiao - 11-23-2020

This is my symple model:

<?php namespace App\Models;

use CodeIgniter\Model;


class banner extends Model {

$query_ = "SELECT img_banner FROM tab_4 WHERE m = 12";

$query = $db->query($query_);

return $query->getResultArray();

}

?>

I receive this error: Undefined variable: db

Which is the correct way for resolve the issue?


RE: Sql in Model - Undefined variable: db - neoneeco - 11-23-2020

https://codeigniter.com/user_guide/database/connecting.html

You have to define db , its in the doc


RE: Sql in Model - Undefined variable: db - wdeda - 11-23-2020

Inside the Model:
PHP Code:
$db $this->db

Outside:
PHP Code:
$db db_connect();