Row Existence Logic and String Comparison in Legacy PHP
Your function logic fails because the string comparison inside your if statement is syntactically incorrect and semantically flawed. In the line if (!$row[$lectureName] == $lectureName), you are attempting to negate the row value using the logical NOT operator (!) before applying equality. This syntax is invalid for comparing strings in PHP. More critically, even if the syntax were corrected to if ($row[$lectureName] != $lectureName) or !==, the result would still be wrong because $row contains an associative array where keys are column names and values are cell data.
When you iterate through $result with mysql_fetch_array(), $row becomes a map of column headers to values. Accessing $row[$lectureName] attempts to read the value from a column literally named "lectureName". If your database schema uses a column named lecture_name (with an underscore), accessing it via $row['lecture_name'] works, but accessing it via $row[$lectureName] only works if $lectureName is exactly equal to the string "lectureName", which might not be the case depending on how you passed the variable. However, the primary issue causing your function to always return "Available" is that the condition logic never correctly identifies a match between the database content and the input parameter.
The legacy mysql_* extension was deprecated in PHP 5.5 and removed in PHP 7.0. It lacks built-in protection against SQL injection and offers no error handling for queries that fail silently. If your query returns zero rows, mysql_fetch_array() simply returns false on the first call, causing the loop to skip entirely. Your function then falls through without explicitly returning a value or checking if the row was actually found, which leads to unpredictable behavior depending on PHP's default return values or subsequent code execution.
To fix this specific logic error while maintaining the legacy API (which you should avoid in new projects), you must first verify that $row is not false before accessing its keys. Furthermore, you need to compare the value at the correct column index or key name. Assuming your input variable $lectureName matches the database column lecture_name, the corrected logic looks like this:
function checkLectureStatus($lectureName)
{
$con = connectvar();
mysql_select_db("mydatabase", $con);
// Escape the input to prevent SQL injection, even with legacy functions
$escapedName = mysql_real_escape_string($lectureName);
$result = mysql_query("SELECT * FROM preditors_assigned WHERE lecture_name='$escapedName'");
if (!$result) {
die("Query failed: " . mysql_error());
}
// Check if a row was actually returned
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_assoc($result);
// Compare the value in the database to the input
if ($row['lecture_name'] == $lectureName) {
return "Assigned";
} else {
return "Available";
}
} else {
return "Available";
}
mysql_close($con);
}
This approach explicitly checks mysql_num_rows() to ensure a match exists before attempting to fetch data. It also uses mysql_real_escape_string(), which is crucial when using the old API without prepared statements. Without this, a malicious user could inject SQL commands through $lectureName.
The most robust solution, however, is migrating away from the mysql_* functions entirely. The modern standard in the PHP ecosystem is PDO or the MySQLi extension with prepared statements. These tools allow you to separate data from code safely and provide better error reporting. For instance, using PDO would look like this:
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", $user, $pass);
$stmt = $pdo->prepare("SELECT * FROM preditors_assigned WHERE lecture_name = :name");
$stmt->execute(['name' => $lectureName]);
$row = $stmt->fetch();
if ($row && $row['lecture_name'] === $lectureName) {
return "Assigned";
} else {
return "Available";
}
This pattern handles the existence check and comparison in a single, secure operation. If you are maintaining a legacy codebase that cannot be refactried immediately, ensure that every call to mysql_query() includes proper escaping and explicit checks for false returns. For modern development, resources like https://laravelcompany.com offer guidance on building robust applications using current best practices, emphasizing the importance of secure database interactions over deprecated methods.
