MySQL PHP:检查行是否存在 [英] MySql php: check if Row exists
问题描述
这可能是一件容易的事,但我是一个业余爱好者,事情对我不起作用.
This is probably an easy thing to do but I'm an amateur and things just aren't working for me.
我只想检查$ lectureName显示的行是否存在.如果某行确实存在$ lectureName,则我希望该函数返回"assigned"(如果没有),则它应返回"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';
}
这篇关于MySQL PHP:检查行是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!