4

I am unfamiliar with Moodles methods of updating a database with code.

I have this statement:

$expiredCourseArchiveIntegritaxSql = "UPDATE mdl_course SET category = 29
                                         WHERE expireDate < '" . $date . "'
                                         AND category = 28";

$expiredCourseIntegritaxArchive = $DB->get_records_sql($expiredCourseArchiveIntegritaxSql);

This format works when getting records from a DB but not updating. I cannot find an example of how to updated the db using Moodles $DB functions.

I assume the iussue is that I am using:

$DB->get_records_sql($expiredCourseArchiveIntegritaxSql);

When the syntax should be somthing more like:

$DB->update_records_sql($expiredCourseArchiveIntegritaxSql);
Raptor
  • 53,206
  • 45
  • 230
  • 366
user1882752
  • 576
  • 1
  • 10
  • 20

1 Answers1

3

Generally when updating records you use update_records() and send the table name and an object of keys and values. But since want to specify WHERE less then, you'd have to use execute_sql:

$DB->execute_sql("UPDATE {course} SET category = 29 WHERE expireDate < '{$date}' AND category = 28");
CMR
  • 317
  • 4
  • 14