PHP CodeIgniter Learning Notes _php Tutorials

Source: Internet
Author: User
Tags autoload codeigniter
Before using the database, we had better connect the database automatically: config/autoload.php automatically load $autoload [' libraries '] = Array (' database ');
Some common functions

Select data
$this->db->select ();
allows you to write the SELECT section in a SQL query .
$this->db->where ();
$this->db->or_where ();
$this->db->where_in ();
allows you to write the where part in the SQL query , and see the Manual for the remaining various where statements.
$this->db->get ();
Run the SELECT query statement and return the result set. You can get all the data for a table.
$this->db->like ();
$this->db->or_like ();
$this->db->not_like ();
This function allows you to generate a LIKE clause, which is useful for querying, and for the rest of the syntax, see the Manual.
$this->db->order_by ();
helps you set an ORDER by clause.
$this->db->group_by ();
allows you to write the GROUP by section in a query statement :
$this->db->distinct ();
add the "DISTINCT" keyword to the query statement :
$this->db->having ();
allows you to write the having section for your query statement .
$this->db->limit ();
Limit the number of results returned by the query :
$this->db->select_max ();
Write a "select MAX (Field)"for your query.
$this->db->select_min ();
Write a "select MIN (Field)" for your query .
$this->db->select_avg ();
Write a "select AVG (Field)" for your query .
$this->db->select_sum ();
Write a "select SUM (Field)" for your query .
$this->db->join ();
allows you to write a JOIN part in a query .
$this->db->count_all_results ();
allows you to obtain the number of results returned by a particular Active Record query. You can use the Active Record to restrict functions such as where (), Or_where (), like (), Or_like () , and so on.

Inserting data

$this->db->insert ();
Generate a SQL insert string based on the data you provide and execute the query. You can pass an array or an object to a function.
$this->db->insert_batch ();
Insert multiple data at a time to generate a SQL insert string based on the data you provide and execute the query. You can pass an array or an object to a function.
$this->db->set ();
This function enables you to set inserts ( insert ) or updates ( update ) values. It can be used instead of a way to directly pass an array to the INSERT and update functions.

Update data

$this->db->update ();
Generates and executes an update ( update ) statement based on the data you provide . You can pass an array or an object to this function.
$this->db->update_batch ();
Generates an update string based on the data you supply, and runs the query. You can either pass a array or an object to the function. A example using an array:

Delete data

$this->db->delete ();
Generates and executes a delete ( delete ) statement.
$this->db->empty_table ();
Generates and executes a delete ( delete ) statement.
$this->db->truncate ();
Generates and executes a TRUNCATE ( truncated ) statement.

Chained method

Chained methods allow you to simplify your syntax in a way that connects multiple functions. Consider this example :
$this->db->select (' title ')->from (' MyTable ')->where (' id ', $id)->limit (ten);
$query = $this->db->get ();
description : Chained methods can only be run under PHP 5 .

Enquiry

$this->db->query ();
to submit a query, use the following function:
$this->db->query (' YOUR query here ');
The query () function Returns a database result set in the form of an object. when you run a query using "read" mode , You can use the " Show your result set " to display the query results ; when using "Write" mode to run the query , will only be returned based on the success or failure of the execution TRUE or FALSE.

Escape Query

$this->db->escape () This function will determine the data type so that only the string type data is escaped. Also, it automatically encloses the data in single quotes, so you don't have to manually add single quotes, as follows: $sql = "INSERT into table" VALUES (". $this->db->escape ($ title). ")";

Query Helper Functions

$this->db->insert_id ()
this ID number is the ID that is inserted when the data is executed .
$this->db->affected_rows ()
The number of rows affected is displayed when a query is performed on a write operation (insert,update , etc.).
$this->db->count_all ();
calculates the total number of rows for the specified table and returns. The name of the submitted table is written in the first argument.

generating a query record set

result ()
The method executes successfully returns an object array, and fails to return an empty array.
Result_array ()
when the method executes successfully, the recordset is returned as an associative array. Returns an empty array upon failure.
row ()
The function returns the first row of data for the current request as an object .

You can pass parameters ( The index of a row ) to get the data for a row. For example, we want to get data for line 5 : $row = $query->row (4);

Row_array ()
function is the same as row () , except that the function returns an array .

In addition , We can use the following method to obtain a record through a cursor:
$row = $query->first_row ()
$row = $query->last_row ()
$row = $query->next_row ()
$row = $query->previous_row ()
by default they will return an object, and you can also pass the parameter "array" to Get the data using the array method $row = $query->first_row (' array ')
$row = $query->last_row (' array ')
$row = $query->next_row (' array ')
$row = $query->previous_row (' array ')

result set helper functions

$query->num_rows ()
the function will return the number of rows currently requested.
$query->num_fields ()
The function returns the number of fields currently requested (number of columns):
$query->free_result ()
This function frees the memory occupied by the current query and deletes its associated resource identity.

Automatic connection

the Auto connect feature automatically instantiates the database class each time a page is loaded. To enable Auto Connect , you can use the application/config/autoload.php Add database in the library array :
$ autoload[' Libraries ' = array (' database ');

Manual Connection

if only a subset of the pages require a database connection, you can manually add the following code to your desired function or manually add it to your class.
$this->load->database ();

Connecting multiple Databases

If you need to connect more than one database at a time, you can do this in the following ways:
$DB 1 = $this->load->database (' Group_one ', TRUE);
$DB 2 = $this->load->database (' Group_two ', TRUE);

table Data

$this->db->list_tables ();
returns an array that contains all the table names in the currently connected database.
$this->db->table_exists ();
Sometimes it is useful to use this function to determine whether a specified table exists before performing an operation on a table. Returns a Boolean value .

Database Tools Classes

Important: Before initializing the Database Tools class, your database driver must already be running because the tool class relies on this.
load Tool class: $this->load->dbutil ()
once initialized, you can access member functions by $this->dbutil objects:
$this->dbutil->list_databases ()
$this->dbutil->database_exists ();
$this->dbutil->xml_from_result ($db _result)
$this->dbutil->backup ()

Database Cache Classes

The

activation cache requires three steps:
1 , Create a writable directory on the server to save the cache file.
2 , Set its directory in the file application/config/database.php $db [' xxxx '] [' cachedir '] .
3 , Activate the cache feature to set global options in file application/config/database.php $db [' xxxx '] [' cache_on ']= ' TRUE ' , or it can be set manually by the method below this page. Once the
is activated, the cache automatically occurs every time that a page containing a database query is loaded.

when there is a database update, we need to delete the cache file
$this->db->cache_delete ()
Delete cached files with specific pages. If you need to clear the cache, update your database
$this->db->cache_delete ('/blog ', ' comments ');
Note that the manual is written $this->db->cache_delete (' blog ', ' comments '); But according to the actual test, you should put a slash before the controller name . '/' to execute correctly.
$this->db->cache_delete_all ()
clears all cache files.

Database Maintenance Classes

Note : to initialize a database maintenance class, make sure that your database driver is running because the class is dependent on the database driver.
use the following method to load the database maintenance class :
$this->load->dbforge ()
once initialized, you can access the functions in the class using the $this->dbforge object :
$this->dbforge->create_database (' db_name ')
allows you to create a database specified by the first parameter.
$this->dbforge->drop_database (' db_name ')
allows you to delete a database specified by the first parameter.
$this->dbforge->create_table (' table_name ');
Once you have declared the fields and keys, you can create a table .

http://www.bkjia.com/PHPjc/327664.html www.bkjia.com true http://www.bkjia.com/PHPjc/327664.html techarticle before using the database, we had better connect the database automatically: config/autoload.php automatically load $autoload [' libraries '] = Array (' database '); Some common functions select data ...

  • Related Article

    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.