discuz! x/Database db:: function Operation method

Source: Internet
Author: User

Db::table ($tablename) Gets the correct prefixed table name, transforms the database handle,

DB::d elete ($tablename, condition, number limit) to delete data from the table

Db::insert ($tablename, data (array), whether the Insert ID is returned, whether the substitution is silent) insert data operation

Db::update ($tablename, data (array) condition) Update operation

Db::fetch (the queried resource) takes an associative array from the result set, noting that if two or more columns in the result have the same field names, the last column takes precedence.

Db::fetch_first ($sql) fetching the first data fetch of the query

Db::fetch_all ($sql) query and Fetch

Db::result_first ($sql) The first field value of a query result set

Db::query ($sql) General Enquiry

Db::num_rows (resource after query) get the record lumped number

Db::_execute (Command, parameter) executes the MySQL class command

Db::limit (n,n) returns the restriction string

Db::field (field name, $pid) returns the condition if the array returns an in condition

Db::order (alias, method) sort



Note: All data needs to be processed addslashes () before inserting the database, so that special characters are not escaped without escaping errors when inserting the database. All variables obtained through GET, POST, FILE, discuz! are escaped by default using Addslashes () and do not have to be repeated. If data processing is necessary (for example, for direct display), you can use Stripslashes () recovery, but the data must be escaped again before inserting the database. In the cache file, the value of the cached data is generally escaped with addcslashes ($string, ' \ \ \ ').


Examples of the most common query databases:

To get a single piece of data:

$rs = Db::fetch_first ("SELECT * from". Db::table (' Common_member '). " WHERE uid=1 ");

This gets the data for the user with UID 1, but there is also a getuserbyuid within the discuz to get the user data

Get more than one piece of data:

$rs = Db::fetch_all ("SELECT * from". Db::table (' Common_member '). " WHERE uid in (1,2,3,4,5,6) ");

Here is the information for the user with UID 1 2 3 4 5 6


Insert data to explain

Call mode: Db::insert ()

Description: about whether or not. As long as the value is 1 is yes, no directly ignore or leave blank

Example:

The first is the existence of a table named Pre_test

The following fields are in the table: ID (increment) name UID field type just leave it.

if (Db::insert (' Test ', array (' ID ' =>null, ' name ' = ' me ', ' uid ' =>1))) showmessage ("Data insertion succeeded");

Note: The key name of the array here is the field name in the data table, and the key value is the value of the corresponding field inserted.



Db::fetch_all (' SELECT * from%t WHERE panel=%d ', Array ($this->_table, $panel), ' uid ');

Db::fetch_all (' SELECT * from%t WHERE uid=%d ORDER by Dateline DESC ', Array ($this->_table, $uid), ' bid '): Array ();

Note: DZ in the% number will be used as parameters, you need to pass in, first you can use Db::table ($this->_table) instead, the second you can be real to indicate that the third word you can calculate the value to fill in, so there should be no problem

discuz! x/Database db:: function Operation method

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.