Home >Backend Development >PHP Tutorial >Collection of commonly used PHP ADODB usage methods_PHP tutorial

Collection of commonly used PHP ADODB usage methods_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:53:35953browse

Copy code The code is as follows:


//Define database variables 
$DB_TYPE = "mysql"; 
$DB_HOST = "localhost"; 
$DB_ USER = "root" ;      ion(" $DB_TYPE");//Create a database object 
$db->debug = true;//DEBUG test of the database, the default value is false 
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;//Returned record set form, association Form                                                                                                      OC',2 ; , is a numeric form, that is, the sort order value of the database field. The index in the record set returned by
ADODB_FETCH_ASSOC is the original database field name.
ADODB_FETCH_BOTH and ADODB_FETCH_DEFAULT return the above two at the same time.Some databases do not support it.
An example: $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
🎜> $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')                                                                                                                                                ;'v1')                                                                                 DB_HOST", "$DB_USER", "$DB_PASS", "$DB_DATABASE")) { );                                                            >SelectLimit($sql,$numrows=-1,$offset=-1) $numrows: how many records to fetch, $offset, which number to start fetching from, usually used for paging, or when only fetching a few records Use                                                                                                               $sql)) {//Execute the SQL statement and return the result to the $rs variable      
  echo $db->ErrorMsg();//This is to print the error message          
   $db->Close() ;//Close the database                                                                                                                                                     .'
'; rs->MoveNext();//Point the pointer to the next record, otherwise an infinite loop will occur!
$rs->Close();//Close to release memory   

//Insert new record   
$sql = “Insert table (user_type,username) VALUES (3, 'liucheng’ )"; "
$db->Execute($sql); "

//Update record " db->Execute($sql);                                                                                                                                                                                                                                                                               

//Get a single record                                                                                                                                        // $db-> GetRow($sql), fetch the first record, and return an array, return false                                                   ,password,user_type FROM table Where id=3"; "
$data_ary = $db->GetRow($sql); "
if ($data_ary == false) { "This record was not found" ';
';                                                                          rs = $db->Execute($sql)) {                                               
}
if (!$result = $rs->FetchRow()) {                                                                                          echo $result[ 'username'] . ' ' . $result['password'] . ' ' . $result['user_type'] . '
'; '
}        

// 取单个字段        
//$db->GetOne($sql) 取出第一条记录的第一个字段的值,出错则返回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;//打印出username的值        
/*      
在进行添加,修改,删除记录操作时,      
要对字符串型的字段,使用$db->qstr()对用户输入的字符进行处理,      
对数字型字段,要进行数据判断      
更新记录,注意:这是针对php.ini中,magic_quotes被设置为Off的情况,如果不确定,可以使用      
$db->qstr($content,get_magic_quotes_gpc())      
注意:content= 等号右边,没有单引号      
*/       
$sql = "Update table SET content=" . $db->qstr($content) . " Where id=2";        
$db->Execute($sql);        

       
/*$db->Insert_ID(),无参数,返回刚刚插入的那条记录的ID值,仅支持部分数据库,带auto-increment功能的数据库,如PostgreSQL, MySQL 和 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),产生一个ID值.$seqName:用于产生此ID的数据库表名,$startID:起始值,一般不用设置,它会把$seqName中的值自动加1.支持部分数据库,某些数据库不支持      

Insert_ID,GenID,一般我用GenID,使用它的目的,是在插入记录后,要马上得到它的ID时,才用      
*/       
/*Example:      
先创建一个列名为user_id_seq的表,里面只有一个字段,id,int(10),NOT NULL,然后插入一条值为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(),取出记录集总数,无参数      
它好像是把取出的记录集,用count()数组的方法,取得数据的数量      
如果取大量数据,效率比较慢,建议使用SQL里的COUNT(*)的方法      
$sql = "Select COUNT(*) FROM table", 用此方法时,不要在SQL里加ORDER BY,那样会降低执行速度      

Example:      
*/       
$sql = "Select * FROM table orDER BY id DESC";        
if (!$rs = $db->Execute($sql)) {        
    echo $db->ErrorMsg();        
$db->Close();                                                          someone To perform the same loop processing twice for one result set, you can use the following method    
The following is just an example to illustrate the use of $rs->MoveFirst()      
*/   
$sql = "Select * FROM table orDER BY id DESC"; ;
if (!$rs = $db->Execute($sql)) { db->Close(); ; $username_ary[] = $rs->fields['username']                                                                                                                                                                                          rs->fields['Field name'], returns the value in this field     
   $rs->MoveNext();//Point the pointer to the next record, if not used, an infinite loop will occur!
} 
$username_ary = array_unique($username_ary); 

$rs->MoveFirst();//Point the pointer back to the first record 
while (!$rs- > EOF) { echo Field name'], what is returned is the value in this field                                                                                                                            

//When the program on this page completes the operation of the database, $db->Close();
$db->Close();

/* A good method */                                                                                                                                  



http://www.bkjia.com/PHPjc/318637.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/318637.html

TechArticle

Copy the code as follows: ?php //Define database variables $DB_TYPE="mysql"; $DB_HOST="localhost "; $DB_USER="root"; $DB_PASS=""; $DB_DATABASE="ai-part"; require_once("../adodb/adodb.i...





Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn