moodle - database connection failed when running a script - php

So I am setting up a moodle plugin that syncs groups defined in child courses into a meta course. I managed to install it based on the following instruction.
Installation
Copy the metagroups folder into your Moodle /local directory and visit
your Admin Notification page to complete the installation.
After installation you may need to synchronize existing meta-course groups, to do this run the cli/sync.php script (use the --help switch for further instructions on script usage).
So when i tried to run th sync.php script using this command in there terminal
php -f sync.php
it shows me an error message
I found that the line in the script that is causing this issue is the following line
require_once(DIR . '/../../../config.php');
my config.php file looks like this:
I have no idea why this is happening, as all moodle functionality seems to be working, which means database connection is the way it should be. Does anyone know this is happening and a way around this. Thanks in advance.
By the way, my moodle version is 2.6.3 running in localhost on a mac OSX 10.9

I had the same problem, but running moodle with MAMP.
The simple fix for me was just to add the php path for MAMP (for whichever PHP version I required):
export PATH=/Applications/MAMP/bin/php/php5.6.40/bin/:$PATH
…and then run whatever PHP script I needed!

Just a guess but I'm wondering if its picking up a different config.php
Try specifying the folder for the require_once
require_once('/Applications/XAMMP/moodlewebroot/config.php');
If that works then check the check the output of
echo DIR . '/../../../config.php'
Although it's more common to use dirname() in Moodle eg:
require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
Also some of the command line functions expect to have the dirroot set up in config.
$CFG->dirroot = '/Applications/XAMMP/moodlewebroot';

Related

Global Library in Crontab PHP

Is it possible to include global $_lib, $_SETUP; in crontab?
I have a cronjob writting in php file in internet directory (/internet/mycrontab.php), but seem like the crontab throws error when I using $_Lib like in $_lib['db']->db_fetch_object($query).
The $_Lib working fine if I enter the url directly in browser www.myweb.dom/internet/mycrontab.php, and the crontab also working fine if I remove the $_Lib like in $_lib['db']->db_fetch_object($query) by using hardcode sysntax (primary).
If it is possible to include global $_lib, $_SETUP;, how do I correctly do that?
Many thanks for the help.
The problem is that the crontab environment and your web app environment are different things.
The cronjob is run by php-cli while the app is run by apache (or NGINX, whatever) php module.
You should probably evaluate to include your library into the crontab file.
include "/path/to/your/library.php";
$_lib = "whatever";
$_SETUP = "whatever";
Without having a proper look at the code that's the best I can suggest.

cygwin: can't execute PHP script via PATH

I have written a PHP script "toalpha" (purpose not relevant to this question). The script is in $HOME/bin. If my CWD is $HOME/bin, I can execute it by typing "./toalpha". But if I simply type "toalpha", I get a message:
Could not open input file: /c/barry/bin/toalpha
Some information that may be relevant:
. I recently upgraded my desktop from WinXP to Win7.
. This is the first time that I have made my own userid be an ordinary user instead of an admin. (I tried to do that on XP, but ran into problems and went back to having my normal userid be the admin. Don't need a solution for that because I'm no longer running XP and besides it was my own fault.)
. My $HOME is /c/barry
I did that by adding the following to the .bashrc created when I installed cygwin:
mount C: /c
HOME=/c/barry
cd
. .bashrc
. The first two lines of the PHP script are:
#!/c/xampp/php/php
<?php
. Yes, the copy of PHP I want to use is in C:\xampp\php\php.exe
. I have another, similar script that I wrote on WinXP. It starts with the same two lines. It worked fine under XP, but doesn't work on Win7.
My best guesses at the source of the problem:
A difference between XP and Win7
I fumbled the home directory change in some way. IIRC, when I installed cygwin on XP I did some trickery so it would start directly in C:\barry but I couldn't remember how I did that when I installed it on Win7, so I took the "easy way out".
bash is passing to "C:\xampp\php\php.exe"
the file name "/c/barry/bin/toalpha"
but that is a cygwin path and your PHP does not regognize it.
You should convert it in an understandable one using
cygpath
Try
/c/xampp/php/php $(cygpath -w /c/barry/bin/toalpha )

Windows equivalent to #!/usr/bin/php

I am trying to execute a php file from sos-berlin jobscheduler. My OS is windows 7. I am following the documentation
http://www.sos-berlin.com/mediawiki/index.php/How_to_execute_a_PHP_script_with_JS
#!/usr/bin/php
<?php
echo "hello";
?>
But when I executed the script I am getting the error that
'#!' is not recognized as an internal or external command, operable program or batch file.
I came to understand that #!/usr/bin/php is a linux command. I should give here equivalent batch command. I googled, but didn't find any useful result. Please any one help me...
Thanks in advance.
Suppose you have installed php.exe in C:\Program files\MyPhpProgram\php.exe
Then run below 2 lines on terminal (cmd.exe):
assoc .php=phpfile
ftype phpfile="C:\Program files\MyPhpProgram\php.exe" "%1"
Go to your desktop.
Right click My Computer shortcut in the desktop.
Click Properties.
You should see a section of control Panel - Control Panel\System and Security\System.
Click Advanced System Settings on the Left menu.
Click Enviornment Variables towards the bottom of the window.
Select PATH in the user variables list.
Append your PHP Path (C:\myfolder\php) to your PATH variable, separated from the already existing string by a semi colon.
Click OK
Open your "cmd"
Type PATH, press enter
Make sure that you see your PHP folder among the list.
That should work.
install a php engine on windows
in a cmd windows for example, run php <path/to/your/script>
open cmd, go to where you installed php. and run c:\program files\php\php.exe <path/to/your/scirpt>.php
link to setting up the path to include php so you can run it easily from cmd:
http://willj.co/2012/10/run-wamp-php-windows-7-command-line/(dead link)

Warning: exec(): Unable to fork in PHP

I'm using php 5 apache on windows server 2008, I have disabled IIS.
I m using exec command in my PHP script and it was working fine
but today I got an error:
Warning: exec(): Unable to fork
I gave permissions to cmd.exe in C:\Windows\System32 folder
but this did not fix the problem.
Specifically which permissions did you give to who?
Probably not to the right user..
Run this php script:
echo 'Script is executed by: ' . get_current_user() . getmygid();
It will tell you which user is running the PHP scripts and therefore which user to grant permissions to cmd.exe.
In 64-bit windows (e.g server 2008), theres a folder named c:\windows\syswow64, that contains all executables/dll, that are required by a 32 bit app. installed on your 64-bit machine.
Make sure your required 32-bit .exe/dll etc is placed in that folder.
if you cant find it there you will have to put a 32-bit version of the required .exe/dll
there.So this can used by yopur app/process.
Now when your 32-bit application/process executes, windows will automatically redirect your process to execute required app in syswow64 folder.
Hopefully that should resolve your compatibility issue.
I had this problem myself, but was eventually able to resolve it. The problem was due to how I had accidentally set cmd.exe to "Run as Administrator" by default. I say "accidentally" as I thought I was only setting the compatibility settings of a shortcut on my task bar, but it turned-out it was setting the compatibility of the .exe itself. Anyhow, I disabled this by deleting the registry key here:
HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers
After doing this, everything started working again.

how to run a batch file in php under xampp on windows

I'm trying to write a little php to update an svn repo on a server running xampplite under windows. (This is a development server, not a production one.)
Here's my php:
<?php
passthru("update.bat");
// I also tried exec() & putting the svn command in directly
?>
update.bat is sitting in the same folder as the php script
Here's the content of update.bat:
svn up c:\path\to\my\repo
When I run the batch file by itself, it works. When I run it via php, I get this printed to the browser:
C:\path\to\script\folder>svn up c:\path\to\my\repo
which looks good, but the project isn't updated.
Adding the username and password to the batch made the difference. Here's the new update.bat:
svn up --username <usr> --password <pwd> c:\path\to\the\repo
Try this tip on php.net/function.exec
The other option is to manually compile the php svn extension (there's no Windows DLL), but you also need the svn libraries first.

Categories