Check if a row exists using old mysql_* API

前端 未结 6 1471
囚心锁ツ
囚心锁ツ 2020-12-01 15:47

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 "ass

相关标签:
6条回答
  • 2020-12-01 16:23

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

    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";
      }
    }
    
    0 讨论(0)
  • 2020-12-01 16:31
    $result = mysql_query("select if(exists (SELECT * FROM  preditors_assigned WHERE lecture_name='$lectureName'),'Assigned', 'Available')");
    
    0 讨论(0)
  • 2020-12-01 16:33
    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';
    }
    
    0 讨论(0)
  • 2020-12-01 16:34

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

    $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
    }
    
    0 讨论(0)
  • 2020-12-01 16:37

    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';
    }
    
    0 讨论(0)
  • 2020-12-01 16:42

    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.

    0 讨论(0)
提交回复
热议问题