Created
September 12, 2012 01:18
-
-
Save anonymous/3703467 to your computer and use it in GitHub Desktop.
PDO_locks version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function is_course_available($id_course) { | |
$sql = "SELECT id_course FROM students_courses WHERE id_course = $id_course"; | |
$statement = Connection::get_connection()->query($sql); | |
$students_course = $statement->fetchAll(); | |
return count($students_course) < MAX_NUMBER_OF_STUDENTS_PER_COURSE; | |
} | |
function insert_student_course($id_student, $id_course) { | |
$sql = "INSERT INTO students_courses(id_student, id_course) VALUES($id_student, $id_course)"; | |
$statement = Connection::get_connection()->prepare($sql); | |
$statement->execute(); | |
} | |
$student = get_random_student(); | |
$course = get_random_course(); | |
Connection::get_connection()->beginTransaction(); | |
Connection::get_connection()->query("LOCK TABLE students_courses"); | |
if (! is_course_available($course['id'])) { | |
Connection::get_connection()->rollBack(); | |
echo "Not avaialble.\n"; | |
exit; | |
} | |
insert_student_course($student['id'], $course['id']); | |
Connection::get_connection()->commit(); | |
echo "Registered.\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment