It is a long journey to study the CI framework. we hope that new users will not worry about it. let's study it together. CI is called Agile development. it has many advantages. it is said that the research on the CI framework of databases has been a long process. we hope new users will not worry about it. let's learn it together. CI is called Agile development. it has many advantages. it means that database connection and database operations are flexible. below we will show you the most basic code and operations for dealing with databases.
Initialize database class
The following code loads and initializes the database class based on your code:
$ This-> load-> database ();
Once loaded, you can use it anywhere like this:
Note: If all your pages require database class initialization, you can make it automatically loaded.
Standard multi-result query (object form)
$ Query = $ this-> db-> query ('select name, title, email FROM my_table ');
Foreach ($ query-> result () as $ row)
{
Echo $ row-> title;
Echo $ row-> name;
Echo $ row-> email;
}
Echo 'total Results: '. $ query-> num_rows ();
The result () function above returnsObject. Example: $ row-> title
Standard multi-result query (in array format)
$ Query = $ this-> db-> query ('select name, title, email FROM my_table ');
Foreach ($ query-> result_array () as $ row)
{
Echo $ row ['title'];
Echo $ row ['name'];
Echo $ row ['email '];
}
The above result_array () function returns an array with the lower mark. Example: $ row ['title']
Test query results
If your query may not return results, we recommend that you use the num_rows () function to test:
$ Query = $ this-> db-> query ("your query ");
If ($ query-> num_rows ()> 0)
{
Foreach ($ query-> result () as $ row)
{
Echo $ row-> title;
Echo $ row-> name;
Echo $ row-> body;
}
}
Standard single-result query (object form)
$ Query = $ this-> db-> query ('select name FROM my_table LIMIT 1 ');
$ Row = $ query-> row ();
Echo $ row-> name;
The row () function above returnsObject. Example: $ row-> name
Standard single-result query (in array format)
$ Query = $ this-> db-> query ('select name FROM my_table LIMIT 1 ');
$ Row = $ query-> row_array ();
Echo $ row ['name'];
The row_array () function above returnsArray. Example: $ row ['name']
Standard insert)
$ SQL = "INSERT INTO mytable (title, name)
VALUES (". $ this-> db-> escape ($ title).", ". $ this-> db-> escape ($ name ).")";
$ This-> db-> query ($ SQL );
Echo $ this-> db-> affected_rows ();
Quick Query
The quick query class provides us with a way to quickly obtain data:
$ Query = $ this-> db-> get ('Table _ name ');
Foreach ($ query-> result () as $ row)
{
Echo $ row-> title;
}
The get () function above returns all results in the data table. The quick query class provides quick functions for all database operations.
Insert)
$ Data = array (
'Title' => $ title,
'Name' => $ name,
'Date' => $ date
);
$ This-> db-> insert ('mytable', $ data );
// Produces: insert into mytable (title, name, date) VALUES ('{$ title}', '{$ name}', '{$ date }')