Copy Code code as follows:
<?php
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");//Set Up Database objects
$db->debug = Debug test for true;//database, default value is False
$ADODB _fetch_mode = adodb_fetch_assoc;//return recordset form, association form
/***
The form of a recordset returned
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 the adodb.inc.php, are available in the "$ADODB _fetch_mode=2" way
Adodb_fetch_num returns the index of the recordset, which is the numeric form, that is, the sort order value of the database field
ADODB_FETCH_ASSOC the index in the recordset returned, is 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 array returned is: Array ([0]=> ' V0 ', [1] => ' v1 ')
Print_r ($rs 2->fields); # The array returned is: Array ([' col1 ']=> ' v0 ', [' col2 '] => ' v1 ')
***/
Connect the database, method has connect,pconnect,nconnect, general use Connect
if (!@ $db->connect ("$DB _host", "$DB _user", "$DB _pass", "$DB _database")) {
Exit (' <a href= "/" > Server busy, please visit </a> later ");
}
/*
$db-> $rs-> The use of this class
Execute ($sql), executing the $SQL statement in the parameter
Selectlimit ($sql, $numrows =-1, $offset =-1) $numrows: Take a few records, $offset, starting with the first few, usually for pagination, or just a few records
*/
Example: Fetching 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 ()//This is a print error message
$db->close ()//Close Database
Exit ();
}
while (! $rs->eof) {//Traverse Recordset
echo $rs->fields[' username ']. ' <br> ';
Print_r ($rs->fields) try, $rs->fields[' field name '], return the value in this field
$rs->movenext ()//point the pointer to the next record, otherwise 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 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);
Take a single record
$db->getrow ($sql), takes the first record and returns an array, error returns 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 ']. ' <br> ';
}
Another way
$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 ']. ' <br> ';
}
Take a single field
$db->getone ($sql) to remove the value of the first field in the first record, error returns false
$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, or delete a record operation,
To work with a string-type field, use $DB->QSTR () to process the characters entered by the user.
For numeric fields, make data judgments
Update records, note: This is for php.ini, the magic_quotes is set to off, and if unsure, you can use
$db->qstr ($content, GET_MAGIC_QUOTES_GPC ())
Note: There is no single quotation mark to the right of the content= equals
*/
$sql = "Update table SET content=". $db->qstr ($content). "Where id=2";
$db->execute ($sql);
/* $db->insert_id (), no parameters, returns the ID value of the record just inserted, supporting only a subset of databases, databases with auto-increment functions, 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) produces an ID value. $seqName: The name of the database table used to produce this ID, $startID: The starting value, generally not set, it will put the $ The value in Seqname is automatically added to 1. Supports partial databases, some databases do not support
Insert_id,genid, usually I use 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, 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 (), remove 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 a lot of data, the efficiency is slow, it is recommended to use the SQL count (*) method
$sql = "Select COUNT (*) from table", when using this method, do not add an order by in SQL, which reduces 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 cyclic processing two times for a result set, you can use the following method
Here is just an example of 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 ']. ' <br> '//print_r ($rs->fields) try, $rs->fields[' field name '], return the value in this field
$rs->movenext ()//point the pointer to the next record, if not, there will be a dead loop!
}
$username _ary = Array_unique ($username _ary);
$rs->movefirst ()//To refer the pointer back to the first record
while (! $rs->eof) {
echo $rs->fields[' password ']. ' <br> '//print_r ($rs->fields) try, $rs->fields[' field name '], return the value in this field
$rs->movenext ()//point pointer to next record
}
$rs->close ();
When the program on this page, the operation of the database after completion, to $db->close ();
$db->close ();
* * A good way to
if (Isset ($db)) {
$db->close ();
}
?>