SQL不返回记录

时间:2019-05-25 12:58:23

标签: php sql sqlite pdo

我正在使用PHP和PDO从数据库中获取数据。

我向php文件发出GET请求。参数为pagecategorysearchterm

如果categorysearchterm为空或不存在,则将它们设置为空变量。

page既代表我的应用程序搜索页面上的页码,又代表SQL语句中的OFFSET。可以是0或10的倍数,具体取决于GET参数。

由于某种原因,如果我在GET请求中同时具有searchtermcategory值,则按预期返回记录。但是,如果缺少其中任何一个,则SQL语句不会返回任何值。

我已经输出了对数据库的确切SQL请求,然后将其复制到SQLite数据库视图中,并返回正确的值。我已经将$page变量输出到屏幕上,而这正是应该的。

我已经将$sqlSearchTerm输出到屏幕上了(该变量是为在搜索词中添加通配符而创建的),而它正是它应该的样子。

另一个注意事项是,根据收到的GET参数,SQL语句将发生变化。我确保这可以正常工作。

以下是注释最多的代码:

        <?php
    //Start Session
      session_start();
    //Require the database object
      require('dbConn.php');

    //Connect to database
      $dbConn = SQLiteConnection::connect();

    //If category is requested then set category to get, otherwise make category empty
      if (!empty($_GET['category'])) {
        $category = $_GET['category'];
      } else {
        $category = '';
      }

    //If searchterm is requested then set searchterm to get, otherwise make searchterm empty
      if (!empty($_GET['searchterm'])) {
        $search_term = $_GET['searchterm'];
        $sqlSearchTerm = "%".$search_term."%";
      } else {
        $search_term = '';
        $sqlSearchTerm = '';
      }

    //If page is requested then set page to get, otherwise make page 0
      if (!empty($_GET['page'])){
        $page = (int)$_GET['page'] * 10;
      } else {
        $page = 0;
      }

    //If user signed in then set $userEmail to session variable
      if(isset($_SESSION['email'])){
        $userEmail = $_SESSION['email'];
      }

      $filmQuery = "
      SELECT nfc_film.title, nfc_film.film_id, nfc_film.description, nfc_film.release_year, nfc_film.rating, nfc_film.last_update, nfc_category.name
        FROM nfc_film
      JOIN nfc_film_category
        ON nfc_film.film_id = nfc_film_category.film_id
      JOIN nfc_category
        ON nfc_film_category.category_id = nfc_category.category_id ";

      if($search_term && $category){
        $filmQuery .= "
        WHERE
          nfc_film.title LIKE :searchterm
        AND
          nfc_category.name = :category";

      } else if ($search_term && !$category) {
        $filmQuery .= "
        WHERE
          nfc_film.title LIKE :searchterm";
      } else if (!$search_term && $category) {
        $filmQuery .= "
        WHERE
          nfc_category.name = :category";
      }

      $filmQuery .= " LIMIT 10 OFFSET :page";

    //Prepare SQL query for execution
      $sqlGetFilms = $dbConn->prepare($filmQuery);
    //Prepare count statement for execution
      $sqlCountFilms = $dbConn->prepare(
        "SELECT COUNT(*) FROM nfc_film
            JOIN nfc_film_category
              ON nfc_film.film_id = nfc_film_category.film_id
            JOIN nfc_category
              ON nfc_category.category_id = nfc_film_category.category_id
            WHERE
              nfc_film.title LIKE :searchterm
            AND
              nfc_category.name = :category
            ");

    //Prepare actor statement for execution
      $sqlGetActors = $dbConn->prepare(
        "SELECT nfc_actor.first_name, nfc_actor.last_name FROM nfc_film_actor
            JOIN nfc_actor
              ON nfc_film_actor.actor_id = nfc_actor.actor_id
            WHERE film_id = :film_id
            ");

    //Prepare note statement for execution
      $sqlGetNotes = $dbConn->prepare(
        "SELECT user, film_id, comment, lastupdated FROM nfc_note
            WHERE
              film_id = :film_id
            AND
              user = :user
            ");

    //Bind parameters to get films statement
      $sqlGetFilms->bindParam(':searchterm', $sqlSearchTerm);
      $sqlCountFilms->bindParam(':searchterm', $sqlSearchTerm);

      $sqlGetFilms->bindParam(':category', $category);
      $sqlCountFilms->bindParam(':category', $category);

      $sqlGetFilms->bindParam(':page', $page);

    //Execute get films query
      $sqlGetFilms->execute();
      $query = $sqlGetFilms->fetchAll();
    //Execute count query
      $sqlCountFilms->execute();
      $countQuery = $sqlCountFilms->fetchAll();

    //Add count value to query
      $query[] = $countQuery[0];

    //For every index in films query
      for($i = 0; $i < (sizeof($query) - 1); $i++){

    //Search for actors for that film
        $sqlGetActors->bindParam(':film_id', $query[$i]['film_id']);
        $sqlGetActors->execute();
        $actors = $sqlGetActors->fetchAll();
    //Convert film title to ucfirst formatting
        $query[$i]['title'] = ucfirst(strtolower($query[$i]['title']));
    //For every index in actors query
        for ($j = 0; $j < sizeof($actors); $j++){
    //Convert actor fname and lname to ucfirst formatting
          $actors[$j]['first_name'] = ucfirst(strtolower($actors[$j]['first_name']));
          $actors[$j]['last_name'] = ucfirst(strtolower($actors[$j]['last_name']));
        }
    //Push actors to film array
        array_push($query[$i], $actors);
    //Execute notes query
        $sqlGetNotes->bindParam(':film_id', $query[$i]['film_id']);
        $sqlGetNotes->bindParam(':user', $userEmail);
        $sqlGetNotes->execute();
        $notes = $sqlGetNotes->fetchAll();
    //Push notes to films array
        array_push($query[$i], $notes);
      }

    //convert films array to json
      $query = json_encode($query, JSON_PRETTY_PRINT);

    //Return film data
      print_r($query);
    ?>

编辑:代码中还有另外两个部分,用于统计记录数量,并获得有关正在搜索的电影的“注释”。

0 个答案:

没有答案