使用旧的 mysql_* API 检查一行是否存在 [英] Check if a row exists using old mysql_* API

查看:22
本文介绍了使用旧的 mysql_* API 检查一行是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想检查 $lectureName 显示的行是否存在.如果某行确实存在带有 $lectureName 的行,我希望该函数返回已分配"如果不是,那么它应该返回可用".这就是我所拥有的.我很确定它是一团糟.请帮忙.

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 行;如果一行返回,则 $lectureNameAssigned,否则它是 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_* API 检查一行是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆