Common methods for using adodb

Source: Internet
Author: User
The above constants are in adodb. inc. the index in the record set returned by the value ADODB_FETCH_NUM defined in php, that is, the value of the variable $ ADODB_FETCH_MODE, is in the numerical form, that is, the record set format returned by the sort order value of the database field
Define ('adodb _ FETCH_DEFAULT ', 0 );
Define ('adodb _ FETCH_NUM ', 1 );
Define ('adodb _ FETCH_ASSOC ', 2 );
Define ('adodb _ FETCH_BOTH ', 3 );
The above constants are defined in adodb. inc. php, that is, the value that can be set by the variable $ ADODB_FETCH_MODE.
Commonly used: ADODB_FETCH_NUM or ADODB_FETCH_ASSOC
The index in the record set returned by ADODB_FETCH_NUM is in the numerical format, that is, the sort order value of the database fields.
The index in the record set returned by ADODB_FETCH_ASSOC is the field name of the original database.
Both ADODB_FETCH_BOTH and ADODB_FETCH_DEFAULT return the values of ADODB_FETCH_NUM and ADODB_FETCH_ASSOC at the same time, which are not supported by some databases.
An example:


$ ADODB_FETCH_MODE = ADODB_FETCH_NUM;
$ Rs1 = $ db-> Execute ('select * from table ');
$ ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
$ Rs2 = $ db-> Execute ('select * from table ');
Print_r ($ rs1-> fields); # The returned array is array ([0] => 'v0', [1] => 'v1 ')
Print_r ($ rs2-> fields); # The returned array is: array (['col1'] => 'v0', ['col2'] => 'v1 ')


<? Php
/*
Common methods for using ADODB
Collation: FEIBA Youxia QQ: 8527385 E-mail: liuchengcn # 163.com
If any error occurs, please forgive me and notify me via QQ or email. thank you.
*/
// Define database variables
$ DB_TYPE = "mysql ";
$ DB_HOST = "localhost ";
$ DB_USER = "root ";
$ DB_PASS = "";
$ DB_DATABASE = "ai-part ";
Require_once ("../adodb. inc. php ");
$ Db = NewADOConnection ("$ DB_TYPE"); // create a database object
$ Db-> debug = true; // DEBUG test of the database. this parameter can be set to true during program development. comment out this line in the official version (the default value is false)
$ ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
/*
Returned record set form
Define ('adodb _ FETCH_DEFAULT ', 0 );
Define ('adodb _ FETCH_NUM ', 1 );
Define ('adodb _ FETCH_ASSOC ', 2 );
Define ('adodb _ FETCH_BOTH ', 3 );
The above constants are defined in adodb. inc. php, that is, the value that can be set by the variable $ ADODB_FETCH_MODE.
Commonly used: ADODB_FETCH_NUM or ADODB_FETCH_ASSOC
The index in the record set returned by ADODB_FETCH_NUM is in the numerical format, that is, the sort order value of the database fields.
The index in the record set returned by ADODB_FETCH_ASSOC is the field name of the original database.
Both ADODB_FETCH_BOTH and ADODB_FETCH_DEFAULT return the values of ADODB_FETCH_NUM and ADODB_FETCH_ASSOC at the same time, which are not supported by some databases.
An example:
 
$ ADODB_FETCH_MODE = ADODB_FETCH_NUM;
$ Rs1 = $ db-> Execute ('select * from table ');
$ ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
$ Rs2 = $ db-> Execute ('select * from table ');
Print_r ($ rs1-> fields); # The returned array is array ([0] => 'v0', [1] => 'v1 ')
Print_r ($ rs2-> fields); # The returned array is: array (['col1'] => 'v0', ['col2'] => 'v1 ')
*/
 
// Connect to the database. The methods include Connect, PConnect, and NConnect. generally, Connect. NConnect is used to Connect to a special database.
If (! @ $ Db-> Connect ("$ DB_HOST", "$ DB_USER", "$ DB_PASS", "$ DB_DATABASE ")){
Exit ('server busy, please visit later ');
}
 
/*
$ Db-> $ rs-> usage of this class
Execute ($ SQL, $ inputarr = false), Execute the $ SQL statement in the parameter, followed by the $ inputarr parameter, which is generally not required
SelectLimit ($ SQL, $ numrows =-1, $ offset =-1, $ inputarr = false) $ numrows: get several records, $ offset, starting from the number of records, selectLimit, usually used for paging, or when only a few records are Retrieved
*/
// Example: retrieve multiple records
$ SQL = "SELECT * FROM table ORDER BY id DESC ";
If (! $ Rs = $ db-> Execute ($ SQL) {// Execute the SQL statement and return the result to the $ rs variable.
Echo $ db-> ErrorMsg (); // The error message is printed.
$ Db-> Close (); // Close the database
Exit ();
}
While (! $ Rs-> EOF) {// traverses the record set
Echo $ rs-> fields ['username'].'
'; // Print_r ($ rs-> fields). try $ rs-> fields ['Field name'] and return the value in this field.
$ Rs-> MoveNext (); // point the pointer to the next record. if you don't need it, an endless loop will occur!
}
$ Rs-> Close (); // Close it to release the memory. Close it once after each operation to develop a good habit of programming.
 
// Insert a new record
$ SQL = "INSERT table (user_type, username) VALUES (3, 'lieng eng ')";
$ Db-> Execute ($ SQL );
// Update records
$ SQL = "UPDATE table SET user_type = 3 WHERE id = 2 ";
$ Db-> Execute ($ SQL );
// Delete a record
$ SQL = "DELETE FROM table WHERE id = 2 ";
$ Db-> Execute ($ SQL );
 
// Retrieve a single record
// $ Db-> GetRow ($ SQL): retrieves the first record in SQL and returns an array. If an error occurs, false is returned.
$ SQL = "SELECT username, password, user_type FROM table WHERE id = 3 ";
$ Data_ary = $ db-> GetRow ($ SQL );
If ($ data_ary = false) {// if you use =, it may not be the result you want.
Echo 'This record is not found ';
Exit ();
} Else {
Echo $ data_ary ['username']. ''. $ data_ary ['password'].''. $ data_ary ['User _ type'].'
';
}
// $ Rs is not used here, and $ rs-> Close () is not required ();
// Another method (it is better and convenient to use the above method)
$ SQL = "SELECT username, password, user_type FROM table WHERE id = 3 ";
If (! $ Rs = $ db-> Execute ($ SQL )){
Echo $ db-> ErrorMsg ();
$ Db-> Close ();
Exit ();
}
If (! $ Result = $ rs-> FetchRow ()){
Echo 'This record is not found ';
Exit ();
} Else {
Echo $ result ['username']. ''. $ result ['password'].''. $ result ['User _ type'].'
';
}
 
// $ Db-> GetOne ($ SQL) retrieves the value of the first field of the first record in SQL. If an error occurs, false is returned.
$ SQL = "SELECT COUNT (id) FROM table ";
$ Record_nums = $ db-> GetOne ($ SQL );
Echo $ record_nums;
$ SQL = "SELECT username, password, user_type FROM table WHERE user_id = 1 ";
$ Result = $ db-> GetOne ($ SQL );
Echo $ result; // The value of username in the record.
/*
When adding, modifying, and deleting records, you must use $ db-> qstr () to process string-type fields and numeric fields, data judgment should be performed before
Update record. note: This is the case where magic_quotes is set to Off in php. ini. if you are not sure, you can use
$ Db-> qstr ($ content, get_magic_quotes_gpc ())
Note: content = the right side of the equal sign does not have single quotation marks
*/
$ SQL = "UPDATE table SET content =". $ db-> qstr ($ content). "WHERE id = 2 ";
$ Db-> Execute ($ SQL );
 
 
/* $ Db-> Insert_ID (), no parameter. the ID value of the record just inserted is returned. only some databases and databases with the auto-increment function are supported, such as PostgreSQL, mySQL and MS SQL
*/
// Example:
$ SQL = "INSERT table (user_type, username) VALUES (3, 'lieng eng ')";
$ Db-> Execute ($ SQL );
$ Data_id = $ db-> Insert_ID ();
Echo $ data_id;
 
/* $ Db-> GenID ($ seqName = 'adodbseq ', $ startID = 1) to generate an ID value. $ seqName: name of the database table used to generate this ID. $ startID: start value. Generally, you do not need to set it. it automatically adds the value in $ seqName to 1. some databases are supported. some databases are not supported.
 
Insert_ID, GenID. Generally, I use GenID, which is used only when the ID of a record is to be obtained immediately after it is inserted.
*/
/* Example:
Create a table named user_id_seq with only one field, id, int (10), not null, and insert a record with a value of 0.
*/
$ User_id = $ db-> GenID ('User _ id_seq ');
$ SQL = "INSERT table (id, user_type, username) VALUES (". $ user_id. ", 3, 'lieng eng ')";
$ Db-> Execute ($ SQL );
 
/*
$ Rs-> RecordCount (), retrieves the total number of record sets, no parameter
It seems that the number of retrieved records is obtained using the count () array method.
If a large amount of data is retrieved, the efficiency is slow. we recommend that you use the COUNT (*) method in SQL.
$ SQL = "select count (*) FROM table". when using this method, do not add order by to SQL, which will reduce the execution speed.
 
Example:
*/
$ SQL = "SELECT * FROM table ORDER BY id DESC ";
If (! $ Rs = $ db-> Execute ($ SQL )){
Echo $ db-> ErrorMsg ();
$ Db-> Close ();
Exit ();
}
$ Record_nums = $ rs-> RecordCount ();
 
/*
If you want to perform the same loop processing for a result set twice, you can use the following method:
The following is an example. it only describes how to use $ rs-> MoveFirst ().
*/
$ SQL = "SELECT * FROM table ORDER BY id DESC ";
If (! $ Rs = $ db-> Execute ($ SQL )){
Echo $ db-> ErrorMsg ();
$ Db-> Close ();
Exit ();
}
$ Username_ary = array ();
While (! $ Rs-> EOF ){
$ Username_ary [] = $ rs-> fields ['username']
Echo $ rs-> fields ['username'].'
'; // Print_r ($ rs-> fields). try $ rs-> fields ['Field name'] and return the value in this field.
$ Rs-> MoveNext (); // point the pointer to the next record. if you don't need it, an endless loop will occur!
}
$ Username_ary = array_unique ($ username_ary );
 
$ Rs-> MoveFirst (); // returns the pointer to the first record, with no parameter
While (! $ Rs-> EOF ){
Echo $ rs-> fields ['password'].'
'; // Print_r ($ rs-> fields). try $ rs-> fields ['Field name'] and return the value in this field.
$ Rs-> MoveNext (); // point the pointer to the next record. if you don't need it, an endless loop will occur!
}
$ Rs-> Close ();
 
 
/*
When the program on this page and the operations on the database are completed, $ db-> Close ();
 
*/
 
$ Db-> Close ();
 
/* A good method */
If (isset ($ db )){
$ Db-> Close ();
}
?>

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.