How to run and test MySQL queries in moodle environment (Moodle 2.5)

How to run and test MySQL queries in moodle environment (Moodle 2.5)

by menre ayes -
Number of replies: 4

Hello forum Members,

Somebody help me please.

I want to learn how to write and execute mysql code and query in moodle version 2.5. Can someone direct me to some resources or explain how to do this to me, please?

I am good at using and writing sql on its own. Now, what I do not know how to do is how to write mysql and execute it within the moodle environment.

How do you normally write, execute and view your sql queries in moodle, please?

For example, in a database, if I have a table for user and want to show the results, I could use a code like the one below (I copied it from somewhere), But where and how to I execute it?

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

I will appreciate your help.

Thanks.

Average of ratings: -
In reply to menre ayes

Re: How to run and test MySQL queries in moodle environment (Moodle 2.5)

by Visvanath Ratnaweera -
Picture of Particularly helpful Moodlers Picture of Translators
Just to be sure, you know the two obvious methods, right?

1. mysql client:
$ mysql -u moodle27 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
...
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use moodle27;
Database changed
mysql> SELECT * FROM mdl_user WHERE id = 1;
| id | auth | confirmed | ...
| 1 | manual | 1 | ...


2. phpMyAdmin
Attachment phpmyadmin.png
Average of ratings: Useful (1)
In reply to Visvanath Ratnaweera

Re: How to run and test MySQL queries in moodle environment (Moodle 2.5)

by menre ayes -

Hello Everyone,

Thank you for your help and suggestions. I tried it and everything worked well for me. For example, I ran a query like this below and it worked fine.

SELECT * FROM mdl_user WHERE country LIKE '%canada%';

One more question please. If I want to create a page inside moodle that displays all the country in the user table, how do I use PHP with this query code to show that result? Does moodle have a default page to show the result of mysql queries? Or is there a special way of doing that in moodle, please?

Thanks.