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

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

Andrea Tullo -
回帖数: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?

回复Andrea Tullo

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

Davo Smith -
Core developers的头像 Particularly helpful Moodlers的头像 Peer reviewers的头像 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.

回复Davo Smith

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

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!