Querying the moodle database within moodle - php

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.

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.)

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 '/'.

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}.

Related

Can't get $_SERVER to run properly

I'm trying to execute "opendir" to understand how it works & use it in a recursive page call (I get wrong directories using .. & .).
My folder is constructed as follows:
C:\xampp\htdocs\Ask_Alansky
C:\xampp\htdocs\ is what comes in the XAMPP package, & Ask_Alansky is the folder in which I'm working.
The issue is simply that I cannot get a opendir () to work & open the directory. I'm not sure if I'm misunderstanding the concept, but I expect the code to simply open the folder "Ask_Alansky" & show me the files.
My code is:
<?php
opendir($_SERVER['C:/xampp/htdocs/Ask_Alansky'] . '/index.php');
?>
I get:
Notice: Undefined index: C:/xampp/htdocs/Ask_Alansky in
C:\xampp\htdocs\Ask_Alansky\php_main\Testing.php on line 14
Warning: opendir(/index.php,/index.php): The system cannot find the file specified. (code: 2) in
C:\xampp\htdocs\Ask_Alansky\php_main\Testing.php on line 14
Warning: opendir(/index.php): failed to open dir: No such file or directory in
C:\xampp\htdocs\Ask_Alansky\php_main\Testing.php on line 14
What I'm doing wrong?
$_SERVER is an associative array that contains multiple server information. Therefore, there is not such key C:/xampp/htdocs/Ask_Alansky. If you wish to access the current directory, use getcwd() instead. It is also not a good idea to use absolute path to your file, this will cause a problem when you migrate your app to another server.

No such file or directory error on require_once in php

I am trying to make a login system for my app, and needed to use php for it. However I'm really new to php so I got errors. First of all, I am using a free web hosting for now, so it might be the issue here. That being said, here is the error part of my php:
<?php
require_once 'include/DB_Functions.php';
?>
And it gives me this error:
Warning: require_once(include/DB_Functions.php): failed to open stream: No such file or directory in /srv/disk3/1965965/www/yigittest.atwebpages.com/login.php on line 2
Fatal error: require_once(): Failed opening required 'include/DB_Functions.php' (include_path='.:/usr/local/php-5.4.45/share/pear') in /srv/disk3/1965965/www/yigittest.atwebpages.com/login.php on line 2
However, my include folder contains all the required php files. So what might be the issue here?
Try this demo code or plz share your folder structure
require_once(__DIR__.'/../include/DB_Functions.php');

PHP include file in class, wrong paths

I have class called client, and there i have function, in which i'm trying to include/execute php file, but when i'm doing it all path's in that file which i'm trying to include goes wrong.
class function:
public static function SendSSHCommand($ssh_cmd) {
include('../Core/core_class.php');
$ssh = new samp_panel();
$ssh->SendCommand($ssh_cmd);
}
I would like get a solution, how to include file, that all directory path's in that file wouldn't go wrong.
I get many errors like that:
Warning: include(Net/SSH2.php): failed to open stream: No such file or directory in /home/vlkg/domains/vlkg.lt/public_html/naujas/Core/core_class.php on line 30
Warning: include(Net/SSH2.php): failed to open stream: No such file or directory in /home/vlkg/domains/vlkg.lt/public_html/naujas/Core/core_class.php on line 30
Warning: include(): Failed opening 'Net/SSH2.php' for inclusion (include_path='.:/usr/share/pear:Core/NetModule') in /home/vlkg/domains/vlkg.lt/public_html/naujas/Core/core_class.php on line 30
Fatal error: Class 'Net_SSH2' not found in /home/vlkg/domains/vlkg.lt/public_html/naujas/Core/core_class.php on line 366
I always change my include directory when I run a script. For me it just makes it easier if I set a point of reference for all my included files.
http://php.net/manual/en/function.set-include-path.php
Put something like this at the top of your file
set_include_path('/usr/www/2/main');
include('include1.php');
include('subdir_of_include_path/include2.php');
There are several options but this is the one I find useful with my projects. This way I force and know what my include path is.

require_once can't find file on webserver but works on localhost

All,
I have the following files:
builder.php
Oauth.php
twitteroauth.php
Within my builder.php page I have the following code:
require_once('twitteroauth.php');
Then in my twitteroauth.php page I have the following code:
require_once('OAuth.php');
All of these files are in the following path: wp-content/themes/alyeska/framework/frontend/functions
I'm testing this on my localhost and it works fine. However, when I upload all of these files to my webserver I get the following error message:
Warning: require_once(OAuth.php) [function.require-once]: failed to open stream: No such file or directory in /home/arokyne/website.com/wp-content/themes/alyeska/framework/frontend/functions/twitteroauth.php on line 10
Fatal error: require_once() [function.require]: Failed opening required 'OAuth.php' (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear') in /home/arokyne/website.com/wp-content/themes/alyeska/framework/frontend/functions/twitteroauth.php on line 10
I'm not sure why I'm getting these errors since it works on my localhost. Can anyone point me in the right direction on how to fix this issue?
Thanks!
Two things jump to mind- make certain your path is correct, including the case sensitivity; also, make certain that the permissions on the file allow for the php server to read the file. A 644 mode should be sufficient, I believe; but, worst case, a 755 would definitely work.
And finally you may want to check whether your include path is correct.

PHP link not working

I'm trying to require_once a file in a document; here's my current syntax:
$path = $_SERVER['DOCUMENT_ROOT'];
require_once("includes/save-email.php");
The problem is that, when I reload my page in my browser, every element disappears and I get the following two messages:
Warning: require_once(/includes/connect.php) [function.require-once]: failed to open stream: No such file or directory in /homepages/3/[my db name]/htdocs/includes/save-email.php on line 2
Fatal error: require_once() [function.require]: Failed opening required '/includes/connect.php' (include_path='.:/usr/lib/php5') in /homepages/3/[my db name]/htdocs/includes/save-email.php on line 2
I've tried every variation of the link I can think of, including ../link, ./link, /link, and link.
Strangely enough, when I include an element of the layout using the same overall syntax (replacing require_once with include), that element loads without any errors, even though it's in the same directory.
I'm not sure that it matters, but my server is run by 1&1.
require_once(/includes/connect.php) will look for the file in the root of your server not in the current folder
repalace
require_once(/includes/connect.php)
by
require_once(includes/connect.php)
in save-email.php on line 2
setting $path won't do anything. you will need to have the includes folder in the same folder as this php document.

Categories