CodeIgniter data function classes in \system\database\db_active_rec.php
Copy Code code as follows:
<span style= "FONT-SIZE:16PX;" >class ModelName extends Ci_model
{
function __construct ()
{
Parent::__construct ();
}
}</span>
Connection database: $this->load->database ();
Copy Code code as follows:
<span style= "FONT-SIZE:16PX;" >classmodel_name extends Ci_model
{
function __construct ()
{
Parent::__construct ();
$this->load->database ();
}
}</span>
Written in the constructor of the model, it is convenient to load the model and connect the database at the same time.
Inserting data
Copy Code code as follows:
<span style= "FONT-SIZE:16PX;" > $this->db->insert ($tableName, $data);</span>
$tableName = is the name of the table you want to manipulate.
$data = The data you want to insert, inserted as an array (Key name = Field name, key value = field value, self-added primary key does not need to be written).
Update data
Copy Code code as follows:
<span style= "FONT-SIZE:16PX;" > $this->db->where (' field name ', ' field value ');
$this->db->update (' table name ', modified array of values);</span>
Querying data
Copy Code code as follows:
<span style= "FONT-SIZE:16PX;" > $this->db->where (' field name ', ' field value ');
$this->db->select (' field ');
$query = $this->db->get (' table name ');
Return$query->result ();</span>
Delete data
Copy Code code as follows:
<span style= "FONT-SIZE:16PX;" > $this->db->where (' field name ', ' field value ');
$this->db->delete (' table name ');</span>
Next we're going to call our model in the controller.
Copy Code code as follows:
<span style= "FONT-SIZE:16PX;" > $this->load->model (' model name ')//model name refers to you in <span style= "Color:rgb (255, 0, 0); "> project directory/models/</span> built under the model (same as the filename)
$this-> Model Name-> method name </span>
In order not to be called once in each controller's method. That's what I did.
Copy Code code as follows:
<span style= "FONT-SIZE:16PX;" >
Class Controllername extends Ci_controller
{
function __construct ()
{
Parent::__construct ();
$this->load->model (' model name ');
}
}</span>