Well, it will work to do it the manual way, but it would be better to use the Moodle functions. All the relevant functions are in lib/dmllib.php. (One benefit of this is that your code will probably then work on any of the databases supported by Moodle, rather than just MySQL.)
A simple case like
SELECT * FROM mdl_user WHERE id = 123;
becomes
get_record('user', 'id', 123);
(See, it is taking care of the table prefix for you.)
A more complex example, like
SELECT * FROM mdl_user WHERE (some complicated condition) ORDER BY lastname, firstname, LIMIT 100;
becomes something like (I am too lazy to look up the order of the arguments just now)
get_records_select('user', '(some complicated condition)', 'lastname, firstname', '*', 0, 100);
Anyway, have a look at the functions in dmllib.php (they are now all well PHPdoced). It is probably also worth searching the code to find places where they are called, to get lots of examples of how they are used.
Testing and QA
correct way to make an sql select in moodle
This discussion has been locked so you can no longer reply to it.