CodeIgniter How to use the database class instructions _php tutorial

Source: Internet
Author: User
In CodeIgniter, using a database is a very frequent thing. You can use the framework's own database classes to easily perform database operations

Initialize the database class to load and initialize the database class according to your database configuration: The code is as follows: This->load->database (); You can use it anywhere after it is loaded. Return the query result code as an object 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 code as an array: $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 '];} Result_array above The () function returns an indexed array. For example: $row [' title '] Returns a Data object form: The code is 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: The code is as follows: $query = $this->db->query (' SELECT name from my_table LIMIT 1 '); $row = $query->row_ar Ray (); Echo $row [' name ']; The Row_array () function above returns an array. For example: $row [' name '] Insert data 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 lets you store database connection values (Username: username, password: password, database name: ). The configuration file is located in the following path: The application/config/database.php accessory file is stored in a multidimensional array in the following format: The code is 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 arbitrarily store settings for multiple connection values. Example: If you run multiple environments (development: Dev, Production: authoring, Test: testing, and so on). ), you can establish a separate connection group for each environment and switch directly to the group. For example, to set up a "test" environment, you can do this: the code is 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"; Then, tell the system to use the "test" group, you can set the variable that is located in the configuration file: The code is as follows: $active _group = "Test"; Note: The name "Test" is arbitrary, which allows you to set it free, our primary connection defaults to the name "Default", and of course, you can give it a more meaningful name based on your project. The active record active record class can be globally set through the $active_record variable in the database configuration file (Allow/Disallow True/false (Boolean)). If you don't use this class, You know what? You can reduce the consumption of computer resources during initialization of the database class by setting this variable value to false. $active _record = TRUE; Note: Some codeigniter classes, such as sessions, require active records support when performing some functions. Parameter resolution: hostname-the host name of the database, usually located on this machine, can be represented as "localhost". Username-the user name that needs to be connected to the database. Password-the password for the login database. Database-The names of the databases you need to connect to. Dbdriver-database type. such as: MySQL, Postgres, ODBC and so on. Must be a lowercase letter. Dbprefix-The prefix of the data table when the active record query is run, which allows multiple CodeIgniter programs to be installed on a database. Pconnect-true/false (Boolean)-Use persistent connections. Db_debug-true/false (Boolean)-Displays database error information.Cache_on-true/false (Boolean)-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 used when communicating with the database. Dbcollat-The character rule used when communicating with the database (character collation). Port-the database port number. Currently only used for Postgres drivers. To use this value, you should add a line of code to the database configuration array.

http://www.bkjia.com/PHPjc/745940.html www.bkjia.com true http://www.bkjia.com/PHPjc/745940.html techarticle in CodeIgniter, using a database is a very frequent thing. You can use the framework's own database classes to make it easy for database operations to initialize database classes based on your number ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.