使用旧的 mysql_* API 检查一行是否存在
问题描述:
我只想检查 $lectureName 显示的行是否存在.如果某行确实存在带有 $lectureName 的行,我希望该函数返回已分配"如果不是,那么它应该返回available".这就是我所拥有的.我很确定它是一团糟.请帮忙.
I just want to check and see if a row exists where the $lectureName shows. If a row does exist with the $lectureName somewhere in it, I want the function to return "assigned" if not then it should return "available". Here's what I have. I'm fairly sure its a mess. Please help.
function checkLectureStatus($lectureName)
{
$con = connectvar();
mysql_select_db("mydatabase", $con);
$result = mysql_query("SELECT * FROM preditors_assigned WHERE lecture_name='$lectureName'");
while($row = mysql_fetch_array($result));
{
if (!$row[$lectureName] == $lectureName)
{
mysql_close($con);
return "Available";
}
else
{
mysql_close($con);
return "Assigned";
}
}
当我这样做时,一切都返回可用,即使它应该返回分配.
When I do this everything return available, even when it should return assigned.
答
这应该可以解决问题:只需将结果限制为 1 行;如果一行返回,$lectureName
是 Assigned,否则它是 Available.
This ought to do the trick: just limit the result to 1 row; if a row comes back the $lectureName
is Assigned, otherwise it's Available.
function checkLectureStatus($lectureName)
{
$con = connectvar();
mysql_select_db("mydatabase", $con);
$result = mysql_query(
"SELECT * FROM preditors_assigned WHERE lecture_name='$lectureName' LIMIT 1");
if(mysql_fetch_array($result) !== false)
return 'Assigned';
return 'Available';
}