With this function I show a list of exercises belonging to a course that come from a table called "lessons" with the following important columns:
lessons.id, lessons.number, lessons.course_id entre otras.
all users see the same user_id is adorning one of my tests, to take the value later
public function get_lessons($args = array()) {
global $db;
$user_id = !isset($args['user_id']) ? null : $args['user_id'];
$course_id = !isset($args['course_id']) ? null : $args['course_id'];
$lessons = array();
$get_lessons = $db->query("SELECT * FROM lessons WHERE course_id=$course_id ORDER BY lesson_number DESC") or _error(SQL_ERROR_THROWEN);
if($get_lessons->num_rows > 0) {
while($lesson = $get_lessons->fetch_assoc()) {
$lessons[] = $lesson;
}
}
return $lessons;
}
and I show the results with a
foreach
So far everything is excellent.
The point is that now I would like to show the user if he has already completed this or that exercise, for which the status was previously inserted in the "lessons_completed" table with the following important columns
lessons_completed.lesson_number, lessons_completed.user_id, lessons_completed.course_id entre otras
With this function
public function get_lessons_completed($args = array()) {
global $db;
$user_id = !isset($args['user_id']) ? null : $args['user_id'];
$course_id = !isset($args['course_id']) ? null : $args['course_id'];
$lessons_completed = array();
$get_lessons_completed = $db->query
(
"SELECT
lessons.lesson_id,
lessons.course_id,
lessons.lesson_number,
lessons.lesson_intro,
lessons.lesson_text,
lessons.lesson_date,
lessons.lesson_prev,
lessons.lesson_top,
lessons_completed.lesson_number,
lessons_completed.completed
FROM lessons
WHERE course_id=$course_id
LEFT JOIN lessons_completed ON
lessons.lesson_number = lessons_completed.lesson_number WHERE
user_id=$user_id"
)
or _error(SQL_ERROR_THROWEN);
if($get_lessons_completed->num_rows > 0) {
while($lesson_completed = $get_lessons_completed->fetch_assoc()) {
$lessons_completed[] = $lesson_completed;
}
}
return $lessons_completed;
}
However I get a 500 error and I really do not know what I am doing wrong, or if it is the correct procedure. I hope that some friend can help me, it may be that my mistake is in the second WHERE, but I already remove it and give the same, error, I delete all the LEFT JOIN together with the two queries to the second table and it works again showing only the records such as the first function.