coding question

coding question

by Robert Lefebvre -
Number of replies: 2

When I installed Moodle, I had to set it up with a completely different host than I usually use. To save a few bucks(I was just "testing" moodle,  I use the MySql db on another, remote website.

I got moodle to work and I'm sold.

I'm trying to code some features to it but I can't get connected to the db in my usual method

My usual connect-to-db syntax is

 
>$server = "localhost"; // often localhost

>$username = "blah"; // your MySQL server username

>$password = "blah_blah"; // your MySQL server password

>  $conn = mysql_connect($server,$username,$password) or die ("Could not connect MySQL");   }

$conn gets used in db queries like this

$sql13 = "INSERT INTO blah, blah values blah, blah
                           $result13 = @mysql_query($sql13, $conn) or die("Couldn't execute 'Update line 90' query ".$sql13);

One problem is that while $server is "localhost" when on the same server, coming in from a remote isn't connecting. I think I've tried every combo for a path I can think of so if someone has ideas on path syntax (there isn't much at php.net). 

I couldn't figure out a workaround using moodle's config. I see it uses                                         
$CFG->dbname  $CFG->dbuser    $CFG->dbpass to carry the path, but I couldn't find any place in the script where it inserts into the db to check the path used (I couldn't find anything like

$sql13 = "INSERT INTO blah, blah values blah, blah
                           $result13 = @mysql_query($sql13, $conn) or die("Couldn't execute 'Update line 90' query ".$sql13);

 )

What kind of coding does Moodle use to connect to db

Thanks


                          

 

 

 

Average of ratings: -
In reply to Robert Lefebvre

Re: coding question

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
This would be a perfect time for you to have the developer API documentation which unfortunately isn't finished yet. (If anyone wants to help just start putting things in this wiki: http://moodle.org/mod/resource/view.php?id=354)

What you need to do, though, is use the functions defined in lib/datalib.php to access the database (which may not be MySQL!).

There are many database functions, but these are the main ones:

get_record()
get_records()
insert_record()
update_record()

etc ... see the code for details.

You don't need to do the connection, it's all done for you by Moodle.
In reply to Robert Lefebvre

Re: coding question

by Scott Elliott -

Robert,

If I understand you right, you have the database for Moodle on one server and your webserver with the Moodle source files on another.  If this is the case, I'll bet your problem is not with coding, but with access privileges setup in MySQL for your user account your trying to connect with.  If you did the following from the Installation instructions:

   # mysql -u root -p
   > CREATE DATABASE moodle;
   > GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,INDEX,ALTER ON moodle.*
           TO moodleuser@localhost IDENTIFIED BY 'yourpassword';
   > quit
   # mysqladmin -p reload

Only php code on the same computer as the database server can have access to the data (localhost).  If you want to have the code on a different server than the database, you must use the name of the computer (or I think IP address will work) where the data resides when creating an account.  For instance, let's say you have the web server on www.madeuphost.com and the database on mydatabase.com, then you should execute the following on mydatabase.com:

   # mysql -u root -p
   > CREATE DATABASE moodle;
   > GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,INDEX,ALTER ON moodle.*
           TO moodleuser@www.madeuphost.com IDENTIFIED BY 'yourpassword';
   > quit
   # mysqladmin -p reload

Now you can use:

 $server = "mydatabase.com";
 $username = "moodleuser";
 $password = "yourpassword";

 $conn = mysql_connect($server,$username,$password) or die ("Could not connect MySQL");   }

  $sql13 = "INSERT INTO blah, blah values blah, blah
                           $result13 = @mysql_query($sql13, $conn) or die("Couldn't execute 'Update line 90' query ".$sql13);

If you are spefically asking about Moodle, you'll want to modify config.php like:

$CFG->dbhost    = "mydatabase.com";   // eg localhost or db.isp.com
$CFG->dbname    = "moodle";      // database name, eg moodle
$CFG->dbuser    = "moodleuser";    // your database username
$CFG->dbpass    = "yourpassword";

You know I had this setup months ago to work on separate computers and I think I'm on the right track smiley.gif.    Also, if you are worried about security, you may want to look at SSH tunnels for communication between the servers.