-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmllib2.php
executable file
·66 lines (58 loc) · 2.6 KB
/
dmllib2.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/* Extension library for standard database access library (dmllib.php).
* Fixes bug MDL-10787.
*
* (c) 2007 Gert Sauerstein
*
* Edited by Andre Scherl to use in moodle 2.0 at 20.01.2011
*/
if(!function_exists("recordset_to_array2")) {
/**
* Utility function used by <code>$DB->get_records_sql_by_field()</code>.
* A column in the recordset containing unique values can be specified to use it as the key to the associative array. If there is no key column, NULL can be specified to generate self-increagins keys for the associative array.
*
* @param object $rs An ADODB RecordSet object.
* @param String $key_field Name of a field in the result set that sould be use for the array key. If null (the default) is specified, a generated self-incrementing key is used rather than a field value.
* @return mixed mixed an array of objects, or false if an error occured or the RecordSet was empty.
*/
function recordset_to_array2($rs, $key_field = null) {
global $CFG;
if ($rs && $rs->RecordCount() > 0) {
if ($records = $rs->GetRows()) {
foreach ($records as $record) {
if ($CFG->dbfamily == 'oracle') {
array_walk($record, 'onespace2empty'); // dirty hack for Oracle, @see recordset_to_array()
}
if($key_field) {
$key = $record[$key_field];
$objects[$key] = (object) $record; /// To object
} else {
$objects[] = (object) $record;
}
}
return $objects;
} else {
return false;
}
} else {
return false;
}
}
}
if(!function_exists("get_records_sql_by_field")) {
/**
* Get a number of records as an array of objects.
*
* @param string $sql the SQL select query to execute
* @param String $key_field Name of a field in the result set that should be used for the (array) keys. If null (the default) is specified, a generated self-incrementing key is used rather than a field value.
* @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
* @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
* @return mixed an array of objects, or false if no records were found or an error occured.
*/
function get_records_sql_by_field($sql, $key_field = null, $limitfrom='', $limitnum='') {
global $DB;
$rs = $DB->get_recordset_sql($sql, $params = null, $limitfrom, $limitnum);
return recordset_to_array2($rs, $key_field);
}
}
?>