Extract data from data base

Extract data from data base

by ons ons -
Number of replies: 8

Hi! I just create a new block and i want to display some data from the table course in this new block. as i am a beginner in moodle sad. Can you please explain to me how can extract these data?

thank you a lot. smile

Average of ratings: -
In reply to ons ons

Re: Extract data from data base

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

The Data Manipulation API, used to query the database, is explained in detail on the developer wiki: http://docs.moodle.org/dev/Data_manipulation_API

Average of ratings: Useful (1)
In reply to Mark Johnson

Re: Extract data from data base

by ons ons -

Thank you

But i tried a hundred of time but it didn't work.

I wrote in a new php page inclued in my block: 

<?php
   global $DB;
   $user = $DB->get_record('user', array('id'=>'1'));
   echo $user;

?>

But it didn't display anything!! sad sad  please help me. thank you

In reply to ons ons

Re: Extract data from data base

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Try var_dump($user); Or the Moodle only print_object($user);
In reply to Davo Smith

Re: Extract data from data base

by ons ons -

No it still didn't work!! :S 

Shall i create a clone table in my new block?? or any other solution?sad

thank you

In reply to ons ons

Re: Extract data from data base

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Maybe this is a silly question, but I assume you've included the Moodle config file somewhere in your PHP file?

If your PHP file is: [moodlefolder]/blocks/myblock/myfile.php, then at the start of it you should have a line that looks like this:

<?php
require_once(dirname(__FILE__).'/../../config.php');

Otherwise none of the Moodle functions (including all the $DB stuff) are going to be present in your file. In fact, depending on your PHP error output settings, you should be getting lots of 'undefined variable' errors. Note keeping an eye on your webserver error logs is a good idea here (/var/log/apache2/error.log is a typical location with Linux / Apache, but it will depend on your OS / webserver).

Davo

In reply to Davo Smith

Re: Extract data from data base

by ons ons -

Thank you.

Yep i've put : include_once(dirname(__FILE__).'../config.php');

but when i saw the log file the error is :

[Wed Mar 28 16:50:22 2012] [error] [client 127.0.0.1] PHP Fatal error:  Call to a member function get_record() on a non-object in /var/www/moodle/blocks/essaiblock/mainessai.php on line 9, referer: http://localhost/moodle/admin/index.php?adminedit=1

How can i fixe it. I am sorry I am a newbie that's why!!

Thank you a lot

In reply to ons ons

Re: Extract data from data base

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

$DB does not exist, which means you haven't included the config file correctly.

At the very least it should read:

require_once(dirname(__FILE__).'/../config.php');

(note the extra slash and always use require_once for config.php, so you get an error message immediately if you've mistyped the path)

The above assumes your path is [moodledir]/yourfolder/yourfile.php

If you've got it in a further subfolder (e.g. [moodledir]/yourfolder/yourfolder2/yourfile.php) then you will need to put:

require_once(dirname(__FILE__).'/../../config.php');