CodeIgniter Introduction to the use of database classes _php Tutorial

Source: Internet
Author: User
In CodeIgniter, using a database is a very frequent thing. You can easily perform database operations by using the database class that comes with the framework.

Initializing the Database class

Load and initialize the database classes according to your database configuration:

$this->load->database ();

You can use it anywhere after it is loaded.

Returning query results as objects

$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. Example: $row->title

Returning query results as an array

$query = $this->db->query (' SELECT name, title, email from my_table '); foreach ($query->result_array () as $row) { C3/>echo $row [' title '];    echo $row [' name '];    echo $row [' email '];}

The Result_array () function above returns an array with the subscript. Example: $row [' title ']

Returns a single piece of data

Object form:

$query = $this->db->query (' SELECT name from my_table LIMIT 1 '); $row = $query->row (); Echo $row->name;

The row () function above returns an object. Example: $row->name

Array form:

$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. Example: $row [' name ']

Inserting (insert) data

$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 Insert Method:

$data = Array (               ' title ' = = $title,               ' name ' = = $name,               ' date ' = $date            ); $this->db->insert ( ' MyTable ', $data); Produces:insert into MyTable (title, name, date) VALUES (' {$title} ', ' {$name} ', ' {$date} ')

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: application/config/database.php

The accessory file is stored in a multidimensional array in the following format:

$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:

$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 variables that are located in the configuration file:

$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.

Active Record

The 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 the local computer, 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.

Record it for easy access.

http://www.bkjia.com/PHPjc/752321.html www.bkjia.com true http://www.bkjia.com/PHPjc/752321.html techarticle in CodeIgniter, using a database is a very frequent thing. You can easily perform database operations by using the database class that comes with the framework. Initialize the database class according to your ...

  • 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.