MySql php:检查行是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9953157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:58:20  来源:igfitidea点击:

MySql php: check if Row exists

phpmysql

提问by Jeff

This is probably an easy thing to do but I'm an amateur and things just aren't working for me.

这可能是一件容易做的事情,但我是一个业余爱好者,事情对我不起作用。

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.

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

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.

当我这样做时,一切都返回可用,即使它应该返回分配。

回答by kijin

Easiest way to check if a row exists:

检查行是否存在的最简单方法:

$lectureName = mysql_real_escape_string($lectureName);  // SECURITY!
$result = mysql_query("SELECT 1 FROM preditors_assigned WHERE lecture_name='$lectureName' LIMIT 1");
if (mysql_fetch_row($result)) {
    return 'Assigned';
} else {
    return 'Available';
}

No need to mess with arrays and field names.

无需弄乱数组和字段名称。

回答by quickshiftin

This ought to do the trick: just limit the result to 1 row; if a row comes back the $lectureNameis Assigned, otherwise it's Available.

这应该可以解决问题:只需将结果限制为 1 行;如果一行返回$lectureNameis Assigned,否则它是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';
}

回答by Starx

Use mysql_num_rows(), to check if rows are available or not

使用 mysql_num_rows(),检查行是否可用

$result = mysql_query("SELECT * FROM preditors_assigned WHERE lecture_name='$lectureName' LIMIT 1");
$num_rows = mysql_num_rows($result);

if ($num_rows > 0) {
  // do something
}
else {
  // do something else
}

回答by juergen d

$result = mysql_query("select if(exists (SELECT * FROM  preditors_assigned WHERE lecture_name='$lectureName'),'Assigned', 'Available')");

回答by Hardeep Pandya

If you just want to compare only one row with $lactureName then use following

如果您只想将一行与 $lactureName 进行比较,请使用以下内容

function checkLectureStatus($lectureName)
{
 $con = connectvar();
 mysql_select_db("mydatabase", $con);
 $result = mysql_query("SELECT * FROM preditors_assigned WHERE lecture_name='$lectureName'");
  if(mysql_num_rows($result) > 0)
  {
         mysql_close($con);
         return "Assigned";
  }
  else
  {
        mysql_close($con);
        return "Available";
  }
}

回答by Your Common Sense

function checkLectureStatus($lectureName) {
  global $con;
  $lectureName = mysql_real_escape_string($lectureName);
  $sql = "SELECT 1 FROM preditors_assigned WHERE lecture_name='$lectureName'";
  $result = mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
  if (mysql_fetch_row($result)) {
    return 'Assigned';
  }
  return 'Available';
}

however you have to use some abstraction library for the database access.
the code would become

但是你必须使用一些抽象库来访问数据库。
代码将成为

function checkLectureStatus($lectureName) {
  $res = db::getOne("SELECT 1 FROM preditors_assigned WHERE lecture_name=?",$lectureName);
  if($res) {
    return 'Assigned';
  }
  return 'Available';
}