#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQLserver version for the right syntax to use near 'ON ctx.instanceid = c.id AND ctx.contextlevel = 50) LIMIT 0, 30' at line 1

OK, so you are missing contexts 493, 570, 571 which should correspond to the categories that the problem courses are in. Or perhaps the categories got deleted somehow, but the courses are still there.

I think the easiest way to fix this up is to use a simple script to move these courses into a category that we know is OK.

If you are wise, you will take a backup of everything before going any further.

Save the following script into a file called fixup.php using a plaing text editor.

Upload it to the root folder of your Moodle site (the same folder that contains files colled config.php and pluginfile.php) then visit the URL .../fixup.php with your web browser. Hopefully it will work, but I have to say this is just my best guess. It might be a good idea to set Debugging to developer level before running the script.

The alternative is to read the code, and work out exactly what the move_courses function does, and do the same thing manually in the database. That is more complex, but might be a bit safer.

I got the same issue and its been resolved now by following all the steps. But one critical issue, I can see all the courses, but courses are empty, I have lost all the course data (forums, resources, activities etc.)

Any suggestion on that Moodle data directory contains the data but Moodle is not showing that.

I experienced this exact same error today. The way I fixed it was to move the problematic course out of the category it was in, using the Moodle interface, and into a different category. I then moved it back into the original category and the course displayed correctly. This action must have forced Moodle to create the missing context entries in the DB.

I'm pretty sure this error was caused by deleting some sub-categories.

Hope this helps other with the same issue.

Marty

P.S. Thanks to all who contributed to this thread, it allowed me to diagnose the problem very quickly.

I am having the exact same problem when upgrading from Moodle 2.3 to either 2.4 or 2.5. There are two courses that I can no longer access, though the do appear to be in the database.

I tried the SQL query Tim suggested and only had the one row returned with contextlevel = 10 so it isn't the same issue, just similar. Can someone please help me find the proper SQL query I need to find the error?

Hi, I have the same problem with Moodle 2.5 - Dataform. When I try to add and element to the Dataform module, I got "Can not find data record in database table course_modules." It then redirects me to this page.

line 1384 of /lib/modinfolib.php: call to debugging() line 1803 of /lib/navigationlib.php: call to get_fast_modinfo() line 1870 of /lib/navigationlib.php: call to global_navigation->generate_sections_and_activities() line 420 of /course/format/lib.php: call to global_navigation->load_generic_course_sections() line 1782 of /lib/navigationlib.php: call to format_base->extend_course_navigation() line 1093 of /lib/navigationlib.php: call to global_navigation->load_course_sections() line 3178 of /lib/navigationlib.php: call to global_navigation->initialise() line 717 of /lib/pagelib.php: call to settings_navigation->__construct() line 734 of /lib/pagelib.php: call to moodle_page->magic_get_settingsnav() line 6348 of /lib/adminlib.php: call to moodle_page->__get() line 150 of /report/log/index.php: call to admin_externalpage_setup()

line 1388 of /lib/modinfolib.php: call to debugging() line 1803 of /lib/navigationlib.php: call to get_fast_modinfo() line 1870 of /lib/navigationlib.php: call to global_navigation->generate_sections_and_activities() line 420 of /course/format/lib.php: call to global_navigation->load_generic_course_sections() line 1782 of /lib/navigationlib.php: call to format_base->extend_course_navigation() line 1093 of /lib/navigationlib.php: call to global_navigation->load_course_sections() line 3178 of /lib/navigationlib.php: call to global_navigation->initialise() line 717 of /lib/pagelib.php: call to settings_navigation->__construct() line 734 of /lib/pagelib.php: call to moodle_page->magic_get_settingsnav() line 6348 of /lib/adminlib.php: call to moodle_page->__get() line 150 of /report/log/index.php: call to admin_externalpage_setup()

Notice: Undefined property: stdClass::$format in /var/www/moodle/course/format/lib.php on line 171Format plugin format_ is not found. Using default format_topics

line 118 of /course/format/lib.php: call to debugging() line 179 of /course/format/lib.php: call to format_base::get_format_or_default() line 35 of /course/format/lib.php: call to format_base::instance() line 1807 of /lib/navigationlib.php: call to course_get_format() line 1870 of /lib/navigationlib.php: call to global_navigation->generate_sections_and_activities() line 420 of /course/format/lib.php: call to global_navigation->load_generic_course_sections() line 1782 of /lib/navigationlib.php: call to format_base->extend_course_navigation() line 1093 of /lib/navigationlib.php: call to global_navigation->load_course_sections() line 3178 of /lib/navigationlib.php: call to global_navigation->initialise() line 717 of /lib/pagelib.php: call to settings_navigation->__construct() line 734 of /lib/pagelib.php: call to moodle_page->magic_get_settingsnav() line 6348 of /lib/adminlib.php: call to moodle_page->__get() line 150 of /report/log/index.php: call to admin_externalpage_setup()

We did an upgrade from 2.7.x to 2.8.1, and everything looked fine. Tested out fine on our dev server, and a smooth upgrade on production.

However, we've now heard from students who can't access their courses. Other roles (admin, teacher, a custom librarian role based off of teacher) have no problems.

Students with access issues get the message: "Can not find data record in database table course".

Affected courses have all students locked out. Other courses in the same category don't have a problem.

We're using the Banner LMB enrollment plugin for the current semester, so I thought it might be related to that. But there are past semester courses that were batch enrolled using the manual enrollment process that are also affected.

We're experiencing this issue only with the auto attendance mod/block if that's helps. After the upgrade our site works fine and all the courses load ok, we get the "Can not find data record in database table context" message only in relation to the attendance module.

You need to enable debugging to see where this error refers to, it debug information would tell you from where it comes.

But would be nice if someone (maybe a developer?) who knows could shed some light on how this context generic message is being triggered and what might be the source of problem. Just a bit of help to investigate further..