Need to perform some logic based on the existence of a record in db. Since this function will be called quite often, I’m wondering if the following can be made more efficient…
Also note that the table is an association table(employee_id and department_id together form a composite primary key), so there will only ever be one record that meets the criteria.
Ex:
<?php
function checkEmpDptAssoc($employee_id, $department_id)
{
$sqlQuery = sprintf("SELECT COUNT(*)
FROM employee_department ed
WHERE ed.employee_id = '%d'
AND ed.department_id = '%d'",
$employee_id,
$department_id);
$result = mysql_query($sqlQuery);
$row = mysql_fetch_assoc($result);
if ((int) $row['COUNT(*)'] > 0) {
return true;
} else {
return false;
}
}
?>
EXISTS might be a little quicker since you only need to ensure one exists.