Initializing database classes
Load and initialize database classes according to your database configuration:
Copy Code code as follows:
You can use it anywhere after being loaded.
Return query results as objects
Copy Code 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
Return query results as an array
Copy Code 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 '];
echo $row [' email '];
}
The Result_array () function above returns an array with a subscript. For example: $row [' title ']
Returns a piece of data
Object form:
Copy Code code as follows:
$query = $this->db->query (' SELECT name from my_table LIMIT 1 ');
$row = $query->row ();
Echo $row->name;
The row () function above returns an object. For example: $row->name
Array form:
Copy Code 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 ']
Inserting (insert) data
Copy Code code as follows:
$sql = "INSERT into MyTable (title, name)
VALUES (". $this->db->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 the database connection values (Username: username, password: password, database name: name of databases, etc...) ). The configuration file is located at the following path: application/config/database.php
The accessories file is stored in a multidimensional array of the following format:
Copy Code 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 for using multidimensional arrays is to allow you to randomly store settings for multiple connection values. 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:
Copy Code 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, and you can set the variables in the configuration file:
Copy Code code as follows:
Note: The name "Test" is arbitrary, which allows you to freely set up our primary connection by default using the name "Default" and, of course, you can give it a more meaningful name based on your project.
Active Record
The 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't use this class, What? 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 host name of the database, usually located on this computer, can be represented as "localhost".
Username-the user name that needs to be connected to the database.
Password-login password for the database.
DB-you need to connect to the database name.
Dbdriver-database type. such as: MySQL, Postgres, ODBC and so on. Must be a lowercase letter.
Dbprefix-the prefix of the datasheet when an active record query is run, which allows multiple CodeIgniter programs to be installed on a single database.
Pconnect-true/false (Boolean)-Use persistent connections.
Db_debug-true/false (Boolean)-Displays database error messages.
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 (character collation) that are used to communicate with the database.
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.