PHP CodeIgniter Learning notes _php Tips

Source: Internet
Author: User
Tags autoload codeigniter

Before using the database, we'd better connect the database automatically: config/autoload.php automatically loads $autoload [' libraries '] = Array (' database ');
Some common functions

Select data
$this->db->select ();
Allow you toSqlWrite in QuerySELECTPart.
$this->db->where ();
$this->db->or_where ();
$this->db->where_in ();
Allow you toSqlWrite in QueryWHEREPart, the rest of the variouswherePlease see the Manual for the statement.
$this->db->get ();
Runs the SELECT query statement and returns 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 generateLikeclause is useful for querying, and the rest of the syntax is read in the manual.
$this->db->order_by ();
Help you set up aORDER BYClause.
$this->db->group_by ();
Allows you to write a query in theGROUP byPart:
$this->db->distinct ();
Adding for Query statements"DISTINCT"Key words:
$this->db->having ();
Allows you to write for your query statementHavingPart.
$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 the JOIN part of the 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 ();
Inserts multiple data at a time, generates a SQL insert string based on the data you provide, and executes 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 passing arrays directly to insert and update functions.

Update data

$this->db->update ();
Generate and execute 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 for you supply, and runs the query. Can either pass an array or a object to the function. This is the 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 grammar by connecting multiple functions. Consider this example :
$this->db->select (' title ')->from (' MyTable ')->where (' id ', $id)->limit (10, 20);
$query = $this->db->get ();
Description : Chained methods can only be run under PHP 5 .

Inquire

$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 object. When you run the query using "read" mode , you can use " Show your result set " to display the query results ;  when you run a query by using write mode , only TRUE or FALSE is returned based on the success or failure of the execution .

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, using the following: $sql = "INSERT into table (title) VALUES (". $this->db->escape ($title) .")";

Query Auxiliary functions

$this->db->insert_id ()
This ID number is the ID that executes the data insert .
$this->db->affected_rows ()
displays the number of rows affected after a query that performs a write operation (insert,update, etc.).
$this->db->count_all ();
Calculates the total number of rows in the specified table and returns. Writes the submitted table name in the first argument.

Generate query Recordset

Result ()
The method performs a successful return of an object array, and the failure returns an empty array.
Result_array ()
Returns the recordset as an associative array when the method executes successfully. Returns an empty array when it fails.
Row ()
This function returns the first row of data from the current request as object .

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

The

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

Beyond ,   We can also get records through cursors using the following method:
$row  =  $query->first_row ()
$row  =  $query->last_row ()
$row  =  $query->next_row ()
$row  =  $query->previous_row ()
They will return a   by default, object , You can also pass parameter   array   to get the data using   array  $row  = $ Query->first_row (' array ')
$row  =  $query->last_row (' array ')
$row  =  $query->next_row (' array ')
$row  =  $query->previous_row (' array ')

Result set Auxiliary function

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

Automatic connection

The automatic connection feature will automatically instantiate the database class when each page is loaded. To enable automatic connection , you can add a database in the Library array in application/config/autoload.php :
$autoload[' libraries ' = array (' database ');

Manual connection

If only a portion of the page requires a database connection, you can manually add the following code in the function you need or manually add it to your class for use.
$this->load->database ();

Connecting multiple databases

If you need to connect more than one database at the same time, you can do it 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 containing the names of all the tables 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 Class

Important: Before you initialize 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

activating caching takes three steps:
1 , creating a writable directory on the server to save cached files.
2 , in the file   application/config/database.php  $db [' xxxx '] [' Cachedir '] sets its directory.
3 , Activates the caching feature, and you can set global options $db in file   application/config/database.php  . xxxx ' [' cache_on ']= ' TRUE ' , can also be set manually on the method below this page.
Once activated, the cache automatically occurs every time a page containing a database query is loaded.

When there is a database update, we need to delete the cached file
$this->db->cache_delete ()
Deletes a cached file with a specific Web page. 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 '); However, the actual test should be in front of the controller name with a slash '/' before the correct execution.
$this->db->cache_delete_all ()
Clears all cached files.

Database Maintenance Class

Note :    to initialize the database maintenance class, make sure your database driver is running because the class is dependent on the database driver.
Loads the database maintenance class using the following method :
$this->load->dbforge ()
once initialized, you can use the $this-> DBFORGE&NBSP Object Access class functions :
$this->dbforge->create_database (' db_name ')
Allows you to create a database specified by the first parameter. The
$this->dbforge->drop_database (' db_name ')
allows you to delete the database specified by the first parameter.
$this->dbforge->create_table (' table_name '); After the
declares the fields and keys, you can create a table .   

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.