Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

DML functions - pre 2.0: Difference between revisions

From MoodleDocs
(13 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<p class="note">'''Important note:''' This article contains information valid for versions before Moodle 2.0, so should be considered '''deprecated'''. If you want to see up to date information, please go to [[DML functions|DML functions]] instead.</p>
In this page you'll access to the available functions under Moodle to be able to access to DB data. You should use '''exclusively''' these functions in order to retrieve or modify DB contents because these functions provide an high level of abstraction and guarantee that your DB manipulation will work against different RDBMS.
In this page you'll access to the available functions under Moodle to be able to access to DB data. You should use '''exclusively''' these functions in order to retrieve or modify DB contents because these functions provide an high level of abstraction and guarantee that your DB manipulation will work against different RDBMS.


Line 7: Line 9:
== The functions ==
== The functions ==


=== Seeing if any records exist match a given criteria ===


      '''Seeing if any records exist match a given criteria'''
           o record_exists($table, $field1='', $value1='', $field2='', $value2='', $field3='', $value3='')  
           o record_exists($table, $field1='', $value1='', $field2='', $value2='', $field3='', $value3='')  
             Test whether a record exists in a table where all the given fields match the given values.
             Test whether a record exists in a table where all the given fields match the given values.
Line 16: Line 18:
             Test whether a SQL SELECT statement returns any records.
             Test whether a SQL SELECT statement returns any records.


=== Seeing how many records match a given criteria ===


      '''Seeing how many records match a given criteria'''
           o count_records($table, $field1='', $value1='', $field2='', $value2='', $field3='', $value3='')  
           o count_records($table, $field1='', $value1='', $field2='', $value2='', $field3='', $value3='')  
             Count the records in a table where all the given fields match the given values.
             Count the records in a table where all the given fields match the given values.
Line 25: Line 27:
             Get the result of a SQL SELECT COUNT(...) query.
             Get the result of a SQL SELECT COUNT(...) query.


=== Retrieving a single record ===


      '''Retrieving a single record'''
           o get_record($table, $field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields='*')  
           o get_record($table, $field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields='*')  
             Get a single record as an object
             Get a single record as an object
Line 35: Line 37:




      '''Getting an array of records'''
=== Getting an array of records ===
 
           o get_records($table, $field='', $value='', $sort='', $fields='*', $limitfrom='', $limitnum='')  
           o get_records($table, $field='', $value='', $sort='', $fields='*', $limitfrom='', $limitnum='')  
             Get a number of records as an array of objects.
             Get a number of records as an array of objects.
Line 52: Line 55:




      '''Getting a particular field from the database'''
=== Getting a particular field from the database ===
 
           o get_field($table, $return, $field1, $value1, $field2='', $value2='', $field3='', $value3='')  
           o get_field($table, $return, $field1, $value1, $field2='', $value2='', $field3='', $value3='')  
             Get a single value from a table row where all the given fields match the given values.
             Get a single value from a table row where all the given fields match the given values.
Line 60: Line 64:
             Get a single value from a table.
             Get a single value from a table.


=== Setting a particular field in the database ===


      '''Setting a particular field in the database'''
           o set_field($table, $newfield, $newvalue, $field1, $value1, $field2='', $value2='', $field3='', $value3='')  
           o set_field($table, $newfield, $newvalue, $field1, $value1, $field2='', $value2='', $field3='', $value3='')  
             Set a single field in every table row where all the given fields match the given values.
             Set a single field in every table row where all the given fields match the given values.




      '''Deleting Records'''
===Deleting Records===
 
           o delete_records($table, $field1='', $value1='', $field2='', $value2='', $field3='', $value3='')  
           o delete_records($table, $field1='', $value1='', $field2='', $value2='', $field3='', $value3='')  
             Delete the records from a table where all the given fields match the given values.
             Delete the records from a table where all the given fields match the given values.
Line 73: Line 78:




      '''Inserting Records'''
===Inserting Records===
 
           o insert_record($table, $dataobject, $returnid=true, $primarykey='id')  
           o insert_record($table, $dataobject, $returnid=true, $primarykey='id')  
             Insert a record into a table and return the "id" field if required
             Insert a record into a table and return the "id" field if required




      '''Updating Records'''
===Updating Records===
 
           o update_record($table, $dataobject)  
           o update_record($table, $dataobject)  
             Update a record in a table
             Update a record in a table
Line 84: Line 91:
==See also==
==See also==


* [[DDL functions - pre 2.0]]: Where all the functions used to handle DB objects ([[wikipedia:Data_Definition_Language|DDL]]) are defined.
* [[DDL functions]]: Up to date information about DDL functions.
 
* [[DML functions]]: Up to date information about DML functions.
* [[DDL functions - pre 2.0]]: '''(deprecated!)''' Where all the functions used to handle DB objects ([[wikipedia:Data_Definition_Language|DDL]]) are defined .
* [[DB layer 2.0 migration docs|DB layer 2.0 migration docs]]: Information about how to modify your code to work with the new Moodle 2.0 DB layer.


[[Category:DB]]
[[Category:XMLDB]]
[[Category:XMLDB]]

Revision as of 05:49, 28 April 2014

Important note: This article contains information valid for versions before Moodle 2.0, so should be considered deprecated. If you want to see up to date information, please go to DML functions instead.

In this page you'll access to the available functions under Moodle to be able to access to DB data. You should use exclusively these functions in order to retrieve or modify DB contents because these functions provide an high level of abstraction and guarantee that your DB manipulation will work against different RDBMS.

Where possible, tricks and examples will be documented here in order to make developers's life a bit easier. Of course, feel free to clarify, complete and add more info to all this documentation. It will be welcome, absolutely!

Main info

The functions

Seeing if any records exist match a given criteria

         o record_exists($table, $field1=, $value1=, $field2=, $value2=, $field3=, $value3=) 
           Test whether a record exists in a table where all the given fields match the given values.
         o record_exists_select($table, $select=) 
           Test whether any records exists in a table which match a particular WHERE clause.
         o record_exists_sql($sql) 
           Test whether a SQL SELECT statement returns any records.

Seeing how many records match a given criteria

         o count_records($table, $field1=, $value1=, $field2=, $value2=, $field3=, $value3=) 
           Count the records in a table where all the given fields match the given values.
         o count_records_select($table, $select=, $countitem='COUNT(*)') 
           Count the records in a table which match a particular WHERE clause.
         o count_records_sql($sql) 
           Get the result of a SQL SELECT COUNT(...) query.

Retrieving a single record

         o get_record($table, $field1, $value1, $field2=, $value2=, $field3=, $value3=, $fields='*') 
           Get a single record as an object
         o get_record_select($table, $select=, $fields='*') 
           Gets one record from a table, as an object
         o get_record_sql($sql, $expectmultiple=false, $nolimit=false) 
           Get a single record as an object using an SQL statement


Getting an array of records

         o get_records($table, $field=, $value=, $sort=, $fields='*', $limitfrom=, $limitnum=) 
           Get a number of records as an array of objects.
         o get_records_select($table, $select=, $sort=, $fields='*', $limitfrom=, $limitnum=) 
           Get a number of records as an array of objects.
         o get_records_list($table, $field=, $values=, $sort=, $fields='*', $limitfrom=, $limitnum=) 
           Get a number of records as an array of objects.
         o get_records_sql($sql, $limitfrom=, $limitnum=) 
           Get a number of records as an array of objects.
         o get_records_menu($table, $field=, $value=, $sort=, $fields='*') 
           Get the first two columns from a number of records as an associative array.
         o get_records_select_menu($table, $select=, $sort=, $fields='*') 
           Get the first two columns from a number of records as an associative array.
         o get_records_sql_menu($sql) 
           Get the first two columns from a number of records as an associative array.


Getting a particular field from the database

         o get_field($table, $return, $field1, $value1, $field2=, $value2=, $field3=, $value3=) 
           Get a single value from a table row where all the given fields match the given values.
         o get_field_select($table, $return, $select) 
           Get a single value from a table row where a particular select clause is true.
         o get_field_sql($sql) 
           Get a single value from a table.

Setting a particular field in the database

         o set_field($table, $newfield, $newvalue, $field1, $value1, $field2=, $value2=, $field3=, $value3=) 
           Set a single field in every table row where all the given fields match the given values.


Deleting Records

         o delete_records($table, $field1=, $value1=, $field2=, $value2=, $field3=, $value3=) 
           Delete the records from a table where all the given fields match the given values.
         o delete_records_select($table, $select=) 
           Delete one or more records from a table


Inserting Records

         o insert_record($table, $dataobject, $returnid=true, $primarykey='id') 
           Insert a record into a table and return the "id" field if required


Updating Records

         o update_record($table, $dataobject) 
           Update a record in a table

See also