Check if a row exists using old mysql_* API

前端 未结 6 1491
囚心锁ツ
囚心锁ツ 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: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';
    }
    

提交回复
热议问题