Does $DB->execute() use prepared statements?

Does $DB->execute() use prepared statements?

by Andrea Tullo -
Number of replies: 4

Hi everyone,

pretty straightforward question, does anybody know if the function "$DB->execute()" use prepared statements, or i have to provide a query already prepared?

Average of ratings: -
In reply to Andrea Tullo

Re: Does $DB->execute() use prepared statements?

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

It would be a very rare situation in which you should be using $DB->execute() in Moodle, as there are almost certainly better DB functions you should be using instead.

If you really need to use $DB->execute(), then, as with all Moodle DB calls, you should make sure your table names are surrounded by {} (so that Moodle can automatically insert the correct prefix, e.g. 'mdl_' into the name) and all parameters should be passed in as an array (the second param to $DB->execute) - see https://docs.moodle.org/dev/Data_manipulation_API#Placeholders for more details.

Average of ratings: Useful (1)
In reply to Davo Smith

Ri: Re: Does $DB->execute() use prepared statements?

by Andrea Tullo -
Sadly i have to update a record in a table if a record in another table meets condition, but these 2 tables do not have a connected unique id, so i cannot identify the record when i create the object to pass to the update_record() function (external plugins...)
Thanks a lot for your help, i was able to do what i needed!
Average of ratings: Useful (1)
In reply to Andrea Tullo

Re: Ri: Re: Does $DB->execute() use prepared statements?

by Somnath Roy -

i have a query can i used $DB->query to execute select statement 

In reply to Somnath Roy

Re: Ri: Re: Does $DB->execute() use prepared statements?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Probably $DB->get_records_sql()