Dob common PHP ADODB use method Collection

Source: Internet
Author: User
Copy CodeThe code is as follows:


Defining Database Variables
$DB _type = "MySQL";
$DB _host = "localhost";
$DB _user = "root";
$DB _pass = "";
$DB _database = "Ai-part";
Require_once (".. /adodb/adodb.inc.php ");
$db = Newadoconnection ("$DB _type");//Build Database object
$db->debug = Debug test for true;//database, the default value is False
$ADODB _fetch_mode = adodb_fetch_assoc;//Returns the recordset form, associated form
/***
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, defined in adodb.inc.php, are available in the "$ADODB _fetch_mode=2" way
The index in the recordset returned by Adodb_fetch_num, which is a numeric form, which is the sort order value of the database field
ADODB_FETCH_ASSOC the index in the recordset returned by the original database field name
Adodb_fetch_both and Adodb_fetch_default are both returned at the same time. Some databases do not support
An example:
$ADODB _fetch_mode = Adodb_fetch_num;
$rs 1 = $db->execute (' SELECT * from table ');
$ADODB _fetch_mode = ADODB_FETCH_ASSOC;
$rs 2 = $db->execute (' SELECT * from table ');
Print_r ($rs 1->fields); # The returned array is: Array ([0]=> ' V0 ', [1] = = ' V1 ')
Print_r ($rs 2->fields); # The returned array is: Array ([' col1 ']=> ' v0 ', [' col2 '] = ' v1 ')
***/
Connect to a database with Connect,pconnect,nconnect, generally using connect
if (!@ $db->connect ("$DB _host", "$DB _user", "$DB _pass", "$DB _database")) {
Exit (' Server busy, please wait and visit ');
}
/*
How to use this class, $rs, $db
Execute ($sql), execute the $SQL statement in the parameter
Selectlimit ($sql, $numrows =-1, $offset =-1) $numrows: Take a few records, $offset, from the beginning of the first, usually for paging, or only to take a few records to use
*/
Example: Remove multiple records
$sql = "SELECT * FROM table OrDER by ID DESC";
if (! $rs = $db->execute ($sql)) {//executes the SQL statement and returns the result to the $RS variable
echo $db->errormsg ();//This is a print error message
$db->close ();//Close Database
Exit ();
}
while (! $rs->eof) {//Traverse Recordset
echo $rs->fields[' username '). '
';
Print_r ($rs->fields), $rs->fields[' field name ', returns the value in this field
$rs->movenext ();//Pointing the pointer to the next record, or there is a dead loop!
}
$rs->close ();//close to free memory
Insert new record
$sql = "Insert table (User_type,username) VALUES (3, ' Liucheng ')";
$db->execute ($sql);
Update record
$sql = "Update table SET user_type=3 Where id=2";
$db->execute ($sql);
Deleting records
$sql = "Delete from table Where id=2";
$db->execute ($sql);
Take a single record
$db->getrow ($sql), takes the first record and returns an array with an error returning false
$sql = "Select Username,password,user_type from Table Where id=3";
$data _ary = $db->getrow ($sql);
if ($data _ary = = False) {
Echo ' did not find this record ';
Exit ();
} else {
echo $data _ary[' username '). ' ' . $data _ary[' password '). ' ' . $data _ary[' User_type '). '
';
}
A different approach
$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 ' did not find this record ';
Exit ();
} else {
echo $result [' username ']. ' ' . $result [' Password ']. ' ' . $result [' User_type ']. '
';
}
Take a single field
$db->getone ($sql) takes the value of the first field in the first record and returns False if an error occurs
$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;//print out the value of username
/*
When you add, modify, and delete record operations,
To use $DB->QSTR () to manipulate the character entered by the user in a string-type field,
For the numeric field, the data should be judged
Update record, note: This is for php.ini, Magic_quotes is set to off, if unsure, you can use the
$db->qstr ($content, GET_MAGIC_QUOTES_GPC ())
Note: C
*/
$sql = "Update table SET C
$db->execute ($sql);

/* $db->insert_id (), no parameters, returns the ID value of the record just inserted, supports only partial databases, databases with auto-increment functionality, such as PostgreSQL, MySQL, and MS SQL
*/
Example:
$sql = "Insert table (User_type,username) VALUES (3, ' Liucheng ')";
$db->execute ($sql);
$data _id = $db->insert_id ();
echo $data _id;
/* $db->genid ($seqName = ' adodbseq ', $startID =1), generates an ID value. $seqName: The name of the database table used to generate this ID, $startID: The starting value, generally not set, it will put the $ The value in Seqname automatically adds 1. Partial databases are supported, some databases do not support
Insert_id,genid, generally I use the GenID, the purpose of using it is to insert the record, to get its ID immediately, only use
*/
/*example:
First create a table with a column named User_id_seq, with only one field, Id,int (ten), not NULL, and then 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, ' Liucheng ')";
$db->execute ($sql);
/*
$rs->recordcount (), take out the total number of recordsets, no parameters
It seems to be taking out the recordset, using the Count () array method, to get the number of data
If you take large amounts of data and are inefficient, it is recommended to use the count (*) method in SQL
$sql = "Select COUNT (*) from table", when using this method, do not add an order by in SQL, which will slow down 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 two times for a result set, you can use the following method
Below, just an example, just to illustrate how $rs->movefirst () is used
*/
$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), $rs->fields[' field name '), return the value in this field
$rs->movenext ();//Pointing the pointer to the next record, if not, there will be a dead loop!
}
$username _ary = Array_unique ($username _ary);
$rs->movefirst ();//Pointing the pointer back to the first record
while (! $rs->eof) {
echo $rs->fields[' password '). '
';//print_r ($rs->fields), $rs->fields[' field name '), return the value in this field
$rs->movenext ();//Pointing the pointer to the next record
}
$rs->close ();
When the program on this page, the operation of the database is completed, to $db->close ();
$db->close ();
/* A good way */
if (Isset ($db)) {
$db->close ();
}
?>

The above describes the dob commonly used PHP ADODB use of the collection, including the DOB aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

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