CI database configuration file is/application/config/database.php
//You can create multiple database connection configurations and choose which database connection to use with $active_group$active _group= ' Default ';//configure whether to load the query build class, which defaults to true, usually keeping the default value$query _builder=TRUE;//database connection configuration, you can have multiple connection configurations, the index needs to distinguish between$db[' Default '] =Array( ' DSN ' = ' ', ' hostname ' = ' localhost ',//IP' Username ' = ' root ',//User name' Password ' = ' 123456 ',//Password' Database ' = ' workplatform ',//Database name' Dbdriver ' = ' mysqli ',//What library is used to access the database//currently supports CUBRID,IBASE,MSSQL,MYSQL,MYSQLI,OCI8 ODBC, PDO, Postgre, SQLite, Sqlite3, sqlsrv' Dbprefix ' = ',//Table Prefixes' Pconnect ' =FALSE, ' db_debug ' =TRUE, ' cache_on ' =FALSE,//whether to enable query caching' Cachedir ' = ',//querying the Cache directory' Char_set ' = ' utf8 ', ' dbcollat ' and ' utf8_general_ci ', ' swap_pre ' = ', '//Interchange table prefix, table prefix substitution notation' Encrypt ' =FALSE, ' compress ' =FALSE, ' Stricton ' =FALSE, ' failover ' =Array(), ' save_queries ' =TRUE);
You need to use the loader to load database objects before using the database
$this->load->database ();
After the load is completed, $this->db is the database object, and subsequent data operations are called by the method of this object
First define the SQL statement:
$sql = ' SELECT * from user ';
Call the query method of the DB object for querying
$result $this->db->query ($sql);
The return value $result is an object that can return different forms of results by calling its method, for example: Call its result () method to get the results of the query
$users $result->result ();
At this point, $users is an array of objects, or call its Result_array () method to get the result of the associative array query
$users $result->result_array ();
Call the row () method to return the first record or several records as an object
$users $result->row ();
However, if SQL is an increment, delete, modify statement, the query () method returns True or false, at which point the result of the execution can be obtained by means of the DB method, for example:
$this->db->affected_rows (); // gets the number of rows affected $this->db->insert_id (); // gets the ID of the inserted data
Before you perform a database operation, you need to call the $this->load->database () method, because you do not know whether $this->db was generated before the query, by modifying the/application/config/ autoload.php file that allows CI to load the database automatically
$autoload Array (' database ');
If too many variables in the SQL statement affect the writing of the statement, placeholders can be used to resolve
$data [0] = ' DJ '; $data [1] = ' 123456 '; $sql = "INSERT into user (account, password, usertype, username) VALUES (' 1231 ',?, ' 1 ',?)" ; $result $this->db->query ($sql$data);
The elements in the incoming array are replaced by the SQL in turn, generating the actual SQL statement
Note: When testing this code, due to the wrong side of the field name added ', resulting in an error, warning
CodeIgniter Study Notes (vii) database operations in--CI