1
votes

I am new to moodle. I am trying to query the moodle database using my own PHP scripts. I am using the data manipulation API but i can't seem to fetch anything. What are the steps i should take to accomplish this. I have seen a number of guidelines here and there that I should include the config.php file in moodle so as to access the $DB global variable and hence access the functions of the DML API. The following is my code:

<?php
require '../config.php';
global $DB;
$user= $DB->get_record_sql('SELECT * FROM {mdl_user} WHERE id=?', array(4));
echo mysql_num_rows($user);

?>

I get the following error when I try to run it on TextMate editor:

PHP Warning:  require(../config.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/moodle24/sbs_android_app/database_manipulation.php on line 2
PHP Fatal error:  require(): Failed opening required '../config.php' (include_path='.:') in  /Applications/MAMP/htdocs/moodle24/sbs_android_app/database_manipulation.php on line 2

I will appreciate the help...Thanks.

3

3 Answers

6
votes

This error:

PHP Warning:  require(../config.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/moodle24/sbs_android_app/database_manipulation.php on line 2

means that the script can't find a file called config.php. ../config.php means the script is going to try and open a file in the script's parent directory called config.php. Be sure your script is actually in that location. If it is not, I recommend changing ../config.php to ./config.php and moving your script into the same directory as config.php.

Because your script can't find config.php, it can't connect to the database (config.php should store the credentials, URL, ect.)

0
votes

I think you are giving wrong path. please be sure that the path you are giving is correct like

require_once('path_from_rootdir to config.php');

note: path_from_rootdir to config.php is the path to config.php and each directory between the path separated by '/'.

0
votes

There are two issues with this (as wriiten in the question) four lines code of Moodle-

require '../config.php';

Here code is getting error- "PHP Warning: require(../config.php): failed to open stream" because config.php is being looked into the parent directory & file is not there. Please check the path & put the correct path of config.php.

$user= $DB->get_record_sql('SELECT * FROM {mdl_user} WHERE id=?', array(4));

While working with Moodle DML API, table prefix(by default mdl_) must not be added before the table name within {}. Thus, there must be only {user} in place of {mdl_user}.