Struggling with get_record_sql()

Struggling with get_record_sql()

by Sergi Rodríguez -
Number of replies: 2

Hi again,

Now I'm trying to get some values from my block's database. I tested it with phpmyadmin and it works well, the query it's something like this:

SELECT cm.id, wf.hoursperday

FROM ...blablabla... wf

JOIN ...... cm

...

I get two elements:

id-> 9 hoursperday -> 0.56

id-> 11 hoursperday-> 1.14

 

I do the same query inside moodle, using the $DB->get_record_sql($sql, $params); method

But on this case I only get one element

id-> 9 hoursperday-> 0.56

 

I have read that the get_record_sql() method uses the first field as a key, therefore it overwrites the content if a new key matches an already existent key.

Knowing that, I tried to use the cm.id variable, which is a unique key for every element, but it seems it's not working right since I'm getting just the first element

Am I doing something wrong? Is there any other method better for what I want?

Thank you all, and of course, merry christmas! smile

Average of ratings: -
In reply to Sergi Rodríguez

Re: Struggling with get_record_sql()

by Darko Miletić -

Note the name of the method you are using - get_record_sql . This method retourns ONE record only. If you want to get all records in a table use get_records_sql.

Average of ratings: Useful (1)
In reply to Darko Miletić

Re: Struggling with get_record_sql()

by Sergi Rodríguez -

Oh my god, I can't believe it was that, I was looking everywhere around the code...

 

Thanks for your fast reply, now it works right smile