2013-06-19 72 views
1

我是新來的moodle。我試圖用我自己的PHP腳本查詢moodle數據庫。我正在使用數據操作API,但我似乎無法獲取任何內容。我應該採取什麼措施來實現這一點。我在這裏和那裏看到了很多指導原則,我應該在moodle中包含config.php文件,以便訪問$ DB全局變量,從而訪問DML API的功能。以下是我的代碼:查詢moodle中的moodle數據庫

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

?> 

我收到以下錯誤,當我嘗試在TextMate的編輯器中運行它:

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 

我會欣賞的幫助...謝謝。

回答

5

此錯誤:

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 

意味着腳本無法找到一個名爲config.php文件。 ../config.php表示腳本將嘗試在腳本的父目錄config.php中打開一個文件。確保你的腳本實際上在那個位置。如果不是,我建議將../config.php更改爲./config.php並將腳本移動到與config.php相同的目錄中。

因爲你的腳本不能找到config.php文件,它無法連接到數據庫(config.php文件應保存憑證,網址等。)

+0

該文件實際上是在正確的地方。我不知道我做錯了什麼。這個config.php在我的文件夾之外 –

+0

嗯。嘗試移動它並將路徑更改爲「./config.php」,看看是否有幫助 –

0

我想你給錯誤的道路。請確保你給的路徑是正確的像

require_once('path_from_rootdir to config.php'); 

注:path_from_rootdir到config.php文件是通向config.php文件,並通過「/」分隔路徑之間的每個目錄。

0

有兩個問題與此(如問題wriiten)Moodle-

require '../config.php'; 

下面的代碼越來越無差錯「PHP警告四行代碼:要求(../ config.php文件):未能打開流「,因爲config.php正在被查看到父目錄&文件不存在。請檢查路徑&放置config.php的正確路徑。

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

雖然在Moodle DML API,表前綴(默認mdl_)工作不得在{}中的表名前添加。因此,必須只有{用戶}代替{mdl_user}。