引入
<?php require_once('../config.php'); // config.php under root folder require_once($CFG->dirroot .'/course/lib.php'); require_once($CFG->libdir .'/filelib.php'); //redirect_if_major_upgrade_required();
參數詳解

define('IGNORE_MISSING', 0);//兼容模式如果沒有找到記錄返回false,顯示調試警告如果多個記錄發現 define('IGNORE_MULTIPLE', 1);//與 IGNORE_MISSING相似但如果多個記錄發現不顯示調試警告,不建議使用 define('MUST_EXIST', 2);//顯示一個記錄必須存!在如果發現沒有記錄或者多個記錄會拋出異常 $conditions:可選的數組默認為null $sort:可選排序默認為空 $fields:要查詢的字段(可以是*或者X和字段名;多個用,隔開)。默認為* $limitfrom=0:返回記錄的子集,開始在這一點上(可選)默認為0 $limitnum:返回一個許多記錄組成的子集總共(可選,如果$ limitfrom設置那么它也是必需的)默認為0 $table:要查詢的表名 $select:要查詢的sql片段 $params:一個sql字段值的參數組
1.統計函數
//看到有多少匹配給定標准的記錄
//①count_records:$DB->count_records($table, array $conditions=null)
//統計函數相當於:select count(*) from mdl_user; echo $DB->count_records('user').'<br><br>'; // 查詢有多少用戶
// ②$DB->count_records_select($table, $select, array $params=null, $countitem="COUNT('x')")
// 注意:COUNT('x')等同於COUNT(*) echo $DB->count_records_select('user', 'firstname=?', array('同學'),"COUNT('x')").'<br><br>';
//③$DB->count_records_sql($sql, array $params=null)
$sql_1="select count(id)from {user} where firstname = ? "; echo '<pre>'; echo $DB->count_records_sql($sql_1, array('同學')) .'<br />';
//
2、獲取單個記錄 只能顯示一個記錄相當於加了個limit 1
//
// ①get_record($table, array $conditions, $fields='*', $strictness=IGNORE_MISSING)
//所有在函數中的條件參數都用array(字段名=>字段值) $person = $DB->get_record('user', array('firstname'=>'li'));//如果數據庫中與兩條相同的數據只顯示一條 //get_record :相當於select * from 表名 where firstname='li' limit 1; $aaa=$DB->get_record('user', array('id' =>2), 'lastname,firstname', MUST_EXIST); //相當於select lastname,firstname from mdl_user where id =2 limit 1; //var_dump($aaa);
//② $DB->get_record_select($table, $select, array $params=null, $fields='*', $strictness=IGNORE_MISSING)
$record = $DB->get_record_select('user', "id = ?", array(2),'lastname,firstname',IGNORE_MISSING); //var_dump($record); //相當於select lastname,firstname from mdl_user where id =2 limit 2;
//③ 當使用xxx_sql()函數,表名之間必須附上花括號
$user = $DB->get_record_sql('select * from {user} where id = ?', array(2)); //var_dump($user); //select * from {user} where id = 2 limit 1;
// $DB->get_record_sql($sql, array $params=null, $strictness=IGNORE_MISSING);
//所有在函數$ params參數是用於在SQL語句中填充占位符的值的數組。兩個問號和命名占位符可以使用。需要注意的是名為PARAMS 必須是唯一的,即使傳遞的值是相同的。 //問號占位符 $DB->get_record_sql('SELECT * FROM {user} WHERE firstname = ? AND lastname = ?',array('Martin', 'Dougiamas')); //問號占位符 $DB->get_record_sql('SELECT * FROM {user} WHERE firstname = :firstname AND lastname = :lastname',array('firstname'=>'Martin', 'lastname'=>'Dougiamas'));
//
3、得到一個散列數組記錄 可以顯示多個記錄
//
//①$DB->get_records($table, array $conditions=null, $sort='', $fields='*', $limitfrom=0, $limitnum=0)
$params_a = array('lastname' => '高永紅', 'firstname' =>'同學'); $instances = $DB->get_records('user', $params_a, 'lastname, id desc','id,lastname,firstname');//desc倒序 asc正序 echo '<pre>'; var_dump($instances);
//②$DB->get_records_select($table, $select, array $params=null, $sort='', $fields='*', $limitfrom=0, $limitnum=0)
$ccc=$DB->get_records_select('user', 'lastname=?and firstname=?', array('高永紅','同學'), 'id desc', 'id,lastname,firstname', 0, 0); var_dump($ccc);
//③ $DB->get_records_sql($sql, array $params=null, $limitfrom=0, $limitnum=0);
他的語法如下 */ $bbb=$DB->get_records_sql('select * from {user} where firstname=?', array('同學'),0,0);
出來的結果是數組里面套對象並且每個對象還對應其id值,有段哦少記錄顯示多少出來如:
/*array(52) {
[7]=>
object(stdClass)#179 (53) {
["id"]=>
string(1) "7"
.....
注意get_record_sql不管查到幾條數據只顯示一條數據
//④ $DB->get_records_list($table, $field, array $values, $sort='', $fields='*', $limitfrom='', $limitnum='')
$re = $DB->get_records_list('user', 'id', array(2, 5)); //var_dump($re); //相當於SELECT * FROM {} WHERE course IN (?,?)
//
4、獲取數據一個關聯數組的鍵/值對
//
// ① $DB->get_records_menu($table,array$conditions=null,$sort='',$fields='*',$limitfrom=0,$limitnum=0)
// 如果滿足所有給定的條件,那么會把前兩列的記錄作為一個關聯數組,。 $ddd=$DB->get_records_menu('user',array('lastname' => '高永紅', 'firstname' =>'同學'),'id desc','id,lastname,firstname',0,0); /* 結果如下: array(2) { [59]=> string(9) "高永紅" [58]=> string(9) "高永紅" } */ $ddd=$DB->get_records_menu('user',array('lastname' => '高永紅', 'firstname' =>'同學'),'id desc','username,email,lastname,firstname',0,0); /* array(2) { [11111111]=> string(15) "aaaaaaaa@qq.com" [29481804]=> string(15) "29481804@qq.com" } */
//可以看出不管查詢出多少列(字段)只取前兩列並且將第一列的作為鍵第二列作為值組成一個新數組
//②get_records_select_menu($table, $select, array $params=null, $sort='', $fields='*', $limitfrom=0, $limitnum=0)
$eee=$DB->get_records_select_menu('user','lastname=? and firstname=?',array('高永紅', '同學'),'id desc','username,email,lastname,firstname',0,0); /* array(2) { [11111111]=> string(15) "aaaaaaaa@qq.com" [29481804]=> string(15) "29481804@qq.com" } */
//③$DB->get_records_sql_menu($sql, array $params=null, $limitfrom=0, $limitnum=0)
$sql_f='select username,email,lastname,firstname from {user} where lastname=? and firstname = ?'; $fff=$DB->get_records_sql_menu($sql_f, array('高永紅', '同學'),0,0); /* array(2) { [11111111]=> string(15) "aaaaaaaa@qq.com" [29481804]=> string(15) "29481804@qq.com" } */
//
//如果存在一條記錄返回true不存在則返回false
//
// ①$DB->record_exists($table, array $conditions=null)
$ggg=$DB->record_exists('user', array('id'=>2)); var_dump($ggg);
//② $DB->record_exists_select($table, $select, array $params=null)
$hhh=$DB->record_exists_select('user', 'id=?', array(2)); var_dump($hhh);
//③ $DB->record_exists_sql($sql, array $params=null) (下面這句的sql語句可能不太正確);
$sql_15="select id,lastname,country from {user} where firstname = ? group by country having count(id)>1"; echo '<pre>'; $result15=$DB->record_exists_sql($sql_15, array('同學')) ; var_dump($result15);
//
//得到一個記錄的某個特定字段值
//
//16 $DB->get_field($table, $return, array $conditions, $strictness=IGNORE_MISSING)
// 注意; $conditions查詢出來的值必須是單條記錄多條記錄會報錯(可以這樣認為他們兩個都是獨一無二的)
// 下面這個例子就是從一個叫張麗的字段中查出對應的id
$iii=$DB->get_field('user', 'id', array('lastname'=>'張麗'),IGNORE_MISSING); var_dump($iii);
//17 $DB->get_field_select($table, $return, $select, array $params=null, $strictness=IGNORE_MISSING)
$jjj=$DB->get_field_select('user', 'id', 'lastname=?', array('張麗'), IGNORE_MISSING); var_dump($jjj);
//18 $DB->get_field_sql($sql, array $params=null, $strictness=IGNORE_MISSING)
echo $DB->get_field_sql("select id from {user} where lastname=?", array('張麗'), IGNORE_MISSING).'<br>';
//
//從各個記錄中得到一個特定的字段值
//
//19 $DB->get_fieldset_select($table, $return, $select, array $params=null)
$kkk=$DB->get_fieldset_select('user', 'id','firstname=?', array('同學')); //var_dump($kkk);
//20 $DB->get_fieldset_sql($sql, array $params=null)
$lll=$DB->get_fieldset_sql('select id from {user} where firstname=?', array('同學')); //var_dump($lll);
//
//達到條件時在數據庫中為某個設置特定字段設置新值(修改字段值)修改成功返回true
//
//21 $DB->set_field($table, $newfield, $newvalue, array $conditions=null)
$mmm=$DB->set_field('user', 'username', 'myusername', array('username'=>'11111111')); var_dump($mmm);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+ +
//+ +
//+ 刪除數據 +
//+ +
//+ +
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//刪除單條數據
//22 $DB->delete_records($table, array $conditions=null)
$DB->delete_records('user', array('id'=>60));
//23 $DB->delete_records_select($table, $select, array $params=null)
$DB->delete_records_select('user', 'id=?', array(61));
//刪除多條數據
//24 delete_records_list($table, $field, array $values)
//刪除一個記錄從表中(哪個表)一個字段匹配的值列表
//$field:搜索要刪除的的字段名
//array $values 要刪除的該字段的值的數組
$DB->delete_records_list('user', 'id', array(65,67,68));
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +
// +
// 插入數據 +
// +
// +
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//$DB->insert_record($table, $dataobject, $returnid=true, $bulk=false)
//$dataobject:一個或多個字段的值記錄的數據對象
//$returnid:新創建的記錄條目的id應該返回?如果這個選項不是請求然后返回true / false
// $bulk 是否重復插入
// 返回true或者新的id
//
/*
$person = new StdClass();
$person->auth = 'manual';
$person->confirmed = 1;
$person->mnethostid = 1;
$person->lastlogin = time();
$person->currentlogin = time();
$person->username = "lichihua1";
$person->password = hash_internal_user_password('a173512');
//$person->idnumber = $person->username;
$person->firstname = "李四";
$person->lastname = "老師";
$person->email = "firstname@gmail.com";
$person->city = "蓬溪";
$person->country = "CN";
$person->lang = "zh_cn";
echo $person->id = $DB->insert_record('user', $person);
//插入前insert_record會將上面的數據對象轉化成數組$dataobject = (array)$person;
get_columns($table)方法會將表的字段信息展示出來然后用foreach循環和上面對象轉換的進行比較篩選然過濾后在返回來
然后normalise_value()方法會將返回回來的數組的字段對應的鍵和值進行轉換后再次返回回來
最后再將上面處理的放入insert_record_raw()方法中 進行插入操作
*/
echo '1111111111111111111111111111111111111111<br>';
$record = new stdClass();
$record->auth = 'manual';
$record->confirmed = 1;
$record->mnethostid = 1;
$record->lastlogin = time();
$record->currentlogin = time();
$record->username = "lichihua4";
$record->firstname = "李四4";
$record->lastname = "老師";
$record->password = hash_internal_user_password('123');
//$lastinsertid = $DB->insert_record('user', $record, false);//成功不返回id返回true的操作默認是返回id的
//$lastinsertid = $DB->insert_record('user', $record);//成功返回id的操作默認是返回id的
//var_dump($lastinsertid);
/*
$record1 = new stdClass();
$record1->name = 'overview';
$record1->displayorder = '10000';
$record2 = new stdClass();
$record2->name = 'overview';
$record2->displayorder = '10000';
$records = array($record1, $record2);
$lastinsertid = $DB->insert_records('quiz_report', $records);
*/
/*
//insert_record_raw($table, $params, $returnid=true, $bulk=false, $customsequence=false)
//這個是插入的過程函數不會用到它
//插入新記錄到數據庫,盡可能快,沒有安全檢查,lob不支持
//$params:數據記錄的對象或數組
//$returnid:返回它的插入記錄(return it of inserted record)
// $bulk:重復插入的預期
//$customsequence:如果“id”被包括在$params參數里,那么禁用$returnid
//返回值true或者插入時獲取的id即$customsequence為true是返回id值false是返回true
//
//工作流程:1如果參數不是數組那么經他轉化成數組
//2如果$customsequence為真判斷$params中是否有id如果若果沒有拋出錯誤提示並且將$customsequence的值改為false。如果$customsequence為假按么銷毀$params中的id(unset($params['id'])
//3、判斷參數是否為空為空的話拋出錯誤提示
//4、//取得數組的鍵名並用,將其拼接成字符串
// $qms = array_fill(0, count($params), '?');//用問號填充數組填充次數為參數的個數
// $qms = implode(',', $qms);//將?填充好的數組之后,在用,鏈接成一個字符串
//這時的sql語句為INSERT INTO 表名 (字段名-參數1,參數2,參數3) VALUES(?,?,?...)
//5 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);將處理好的sql語句參數等依次分別付給左邊變量。。。執行插入操作
*/
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+ +
//+ +
//+ 修改(數據)記錄 +
//+ +
//+ +
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//$DB->update_record($table, $dataobject, $bulk=false)
$dataobject = new stdClass();
$dataobject->id = 73;
$dataobject->username = "lichihua004";
$dataobject->firstname = "李四004";
$dataobject->password = hash_internal_user_password('00123');
//$DB->update_record('user', $dataobject, false);
//如果要使用任意的sql執行更復雜的更新
//$DB->execute($sql, array $parms=null)
//不要使用這個使數據庫結構變化,用database_manager方法代替!
//database_manager
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+ +
//+ +
//+ 使用記錄集 +
//+ +
//+ +
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*
其中從DB中檢索的記錄的數量高,get_records_xxx()函數上述遠非最佳的,因為它們加載在存儲器中的所有記錄在同一時間。在這種情況下,強烈建議使用這些get_recordset_xxx()函數來代替,其中用一個很好的機制來遍歷所有的目標記錄,並節省了大量的內存。只有一件事是絕對重要的:不要忘記使用后關閉該記錄集! (這將釋放在RDBMS大量的資源)。下面是遍歷使用get_recordset_xxx()函數記錄的一般方式:
這是一般方法遍歷記錄使用get_recordset_xxx()函數:
$rs=$DB->get_recordset(....){
foreach($rsas$record){
// Do whatever you want with this record
}
$rs->close();// Don't forget to close the recordset!
這是可用功能列表(100%搭配get_records_xxx()以上):
o $DB->get_recordset($table,array$conditions=null,$sort='',$fields='*',$limitfrom=0,$limitnum=0)
/// Get a number of records as a moodle_recordset where all the given conditions met.
o $DB->get_recordset_select($table,$select,array$params=null,$sort='',$fields='*',$limitfrom=0,$limitnum=0)
/// Get a number of records as a moodle_recordset which match a particular WHERE clause.
o $DB->get_recordset_sql($sql,array$params=null,$limitfrom=0,$limitnum=0);
/// Get a number of records as a moodle_recordset using a SQL statement.
o $DB->get_recordset_list($table,$field='',$values='',$sort='',$fields='*',$limitfrom='',$limitnum='')
/// Get a number of records as a moodle_recordset where one field matches one list of values.
與get_record功能,您不能使用rs = = true或$!空(rs)來確定任何記錄被發現。記錄集這樣的實現標准的PHP迭代器接口(http://uk.php.net/manual/en/class.iterator.php)
所以,
不像get_record功能,你不能使用$rs == true 或者 !empty($rs),以確定是否發現任何記錄。記錄集實現標准的PHP Iterator接口(http://uk.php.net/manual/en/class.iterator.php) 所以
if($rs->valid()){
// The recordset contains records.
}
*/
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+ +
//+ +
//+ 委托交易(事務處理) +
//+ +
//+ +
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*
//啟動事物
$transaction=$DB->start_delegated_transaction();
//提交?
$transaction->allow_commit();
//回滾
$transaction->rollback($e);
*/
//例子
/*
global $DB;
try {
$transaction = $DB->start_delegated_transaction();
// Insert a record
$DB->insert_record('foo', $object);
$DB->insert_record('bar', $otherobject);
// Assuming the both inserts work, we get to the following line.
$transaction->allow_commit();
} catch(Exception $e) {
$transaction->rollback($e);
}
*/
//$DB->set_debug(true) 調試功能
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+ +
//+ +
//+ SQL兼容性函數 +
//+ +
//+ +
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//看不懂省略
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+ +
//+ +
//+ 特殊情況 :課程獲取 +
//+ +
//+ +
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//注意:在2.51版本以上生效
//get_course($fields)比如你想要根據課程id查這門課程的記錄那么用get_course($courseid)替換:$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
//get_courses(此括號里無參數):這個函數是獲取當前所有的課程
$courses = get_courses();
var_dump($courses);
echo'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<br />';
$capabilityname='plugintype/pluginname:capabilityname';
$pattern='|[/:]|';
$arr=preg_split($pattern,$capabilityname);//這里是用/或者:分割字符串
var_dump($arr);
list($type, $name, $capname) = preg_split('|[/:]|', $capabilityname);
var_dump($type);
echo '';
//list()把數組中的值付給一些變量
//preg_split(正則模式,要處理的字符串) — 通過一個正則表達式分隔字符串
echo'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<br />';