in CodeIgniter, using a database is a very frequent thing. You can use the database classes from the framework, and you can easily do database operations
Initialize database classes load and initialize database classes according to your database configuration: Code is as follows: This->load->database (); is loaded and you can use it anywhere. Returns query results in object form code as follows: $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 returns an array of objects. For example: $row->title Returns the query result as an array code as follows: $query = $this->db->query (' SELECT name, title, email from my_table '); foreach ($query->result_array () as $row) { echo $row [' title '] echo $row [' name ']; &n Bsp echo $row [' email ']; } the Result_array () function above returns an array with the subscript. For example: $row [' title '] returns a data object form: code as follows: $query = $this->db->query (' SELECT name from my_table LIMIT 1 '); $row = $query->row (); Echo $row->name; above ROThe W () function returns an object. For example: $row->name Array form: Code as follows: $query = $this->db->query (' SELECT name from my_table LIMIT 1 '); $row = $query->row_array (); echo $row [' name ']; The Row_array () function above returns an array. For example: $row [' name '] insert (insert) data code as follows: $sql = "INSERT INTO MyTable (title, name) VALUES (". $this ;d B->escape ($title). "," $this->db->escape ($name). "); $this->db->query ($sql); echo $this->db->affected_rows (); Database configuration CodeIgniter has a configuration file that allows you to store database connection values (Username: username, password: password, database name: DB name, etc...) ). The configuration file is located at the following path: application/config/database.php Accessories files are stored in a multidimensional array in the following format: code as follows: $db [' Default '] [' Hostname '] = "localhost"; $db [' Default '] [' username '] = "root"; $db [' Default '] [' password '] = "; $db [' Default '] [' database '] = "database_name"; $db [' Default '] [' dbdriver '] = "MySQL"; $db [' Default '] [' dbprefix '] = "; $db [' Default '] [' pconnect '] = TRUE; $db [' Default '] [' db_debug '] = FALSE; $db [' Default '] [' cache_on '] = FALSE; $db [' Default '] [' cachedir '] = "; $db [' Default '] [' char_set '] = "UTF8"; $db [' Default '] [' dbcollat '] = "utf8_general_ci"; The reason uses multidimensional arrays is to allow you to store multiple connection-value settings at random. Example: If you are running multiple environments (development: development, Production: production, test: testing, etc...) , you can set up a separate connection group for each environment and switch directly to the group. For example, set up a "test" environment that you can do: code as follows: $db [' Test '] [' hostname '] = "localhost"; $db [' Test '] [' username '] = "root"; $db [' Test '] [' password '] = "; $db [' Test '] [' database '] = "database_name"; $db [' Test '] [' dbdriver '] = "MySQL"; $db [' Test '] [' dbprefix '] = "; $db [' Test '] [' pconnect '] = TRUE; $db [' Test '] [' db_debug '] = FALSE; $db [' Test '] [' cache_on '] = FALSE; $db [' Test '] [' cachedir '] = "; $db [' Test '] [' char_set '] = "UTF8"; $db [' Test '] [' dbcollat '] = "utf8_general_ci"; So, tell the system to use the "test" group, you can set the variables in the configuration file: code is as follows: $active _group = "Test"; NOTE: the name "Test" is arbitrary, which allows you to freely set up our main connection by default using the name "Default" and, of course, you can make a more meaningful name for it based on your project. Active record Active record class can be set globally (Allow/Disallow True/false (Boolean)) through the $active_record variable in the database configuration file. If you don'tWith this class, you can reduce the consumption of computer resources when the database class is initialized by setting the variable value to False. $active _record = TRUE; NOTE: Some codeigniter classes, such as sessions, require active records support when performing functions. Parameter resolution: hostname-the hostname of the database, usually located on this machine, can be represented as "localhost" . username-user name to connect to the database . password-login password for the database. DB-You need to connect the database name . dbdriver-database type. such as: MySQL, Postgres, ODBC and so on. Must be a lowercase letter. Dbprefix-When you run an active record query, the prefix of the datasheet allows you to install multiple CodeIgniter programs . pconnect-true/false (Boolean) on a single database using persistent connections. Db_debug-true/false (Boolean)-Displays database error information . Cache_on-true/false (Boolean)-whether the database query cache is open, see the database Cache class for details. Cachedir-The absolute path of the server where the database query cache directory resides. Char_set-the character set to use when communicating with the database. Dbcollat-The character rules used to communicate with the database (character collation). Port-Database port number. Currently used only for Postgres drivers. To use this value, you should add a line of code to the database configuration array.