How to fetch whole record of a table and display

How to fetch whole record of a table and display

от Ravi kumar -
Количество ответов: 2

How to fetch whole record of a table and display

В ответ на Ravi kumar

Re: How to fetch whole record of a table and display

от Alfie Punnoose -

Hi Ravi,

this documentation should help you: https://docs.moodle.org/dev/Data_manipulation_API


You can use either of this functions

$DB->get_records($table, array $conditions=null, $sort='', $fields='*', $limitfrom=0, $limitnum=0)

eg: $result = $DB->get_records($table,array('foo'=>'bar'),null,'foo,bar,jon,doe');

$DB->get_records_sql($sql, array $params=null, $limitfrom=0, $limitnum=0)

eg: $result = $DB->get_records_sql('SELECT * FROM {table} WHERE foo = ? AND bob = ?', array( 'bar' , 'tom' ));

$result is a array of objects, use foreach to print the records

global $DB; // declare this if within a function

$result = $DB->get_records("user", array()); // get all records in mdl_user table

foreach($result as $record){

     echo $record->username;

}