douxian1892 2019-05-12 05:51
浏览 73

bindParam正在打破SQL查询

I have a query which i am building like so:

$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 0
      ";

This seems to work correctly, i have checked the the variable and ran it in my DBMS and it works.

So then i bind the Params (includes param binds for another query):

  if($search_term != ''){
    $sqlGetFilms->bindParam(':searchterm', $sqlSearchTerm, PDO::PARAM_STR);
    $sqlCountFilms->bindParam(':searchterm', $sqlSearchTerm, PDO::PARAM_STR);
  }

  if($category != ''){
    $sqlGetFilms->bindParam(':category', $category, PDO::PARAM_STR);
    $sqlCountFilms->bindParam(':category', $category, PDO::PARAM_STR);
  }

  if($page != ''){
    $sqlGetFilms->bindParam(':page', $page, PDO::PARAM_INT);
  }

The result that i get when i execute this is nothing.

When i remove the bindParams and replace the parameters in the query with static values, the query returns results.

So, why are the bindParams breaking my query?

EDIT: These are the sql statements for 1. no $search_term or category 2. both $search_term and $category:

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  LIMIT 10 OFFSET 0


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 
    WHERE
      nfc_film.title LIKE :searchterm
    AND
      nfc_category.name = :category LIMIT 10 OFFSET 0
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 搭建pt1000三线制高精度测温电路
    • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
    • ¥15 画两个图 python或R
    • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
    • ¥15 八路抢答器设计出现故障
    • ¥15 opencv 无法读取视频
    • ¥15 用matlab 实现通信仿真
    • ¥15 按键修改电子时钟,C51单片机
    • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
    • ¥20 5037端口被adb自己占了