PDO :: ATTR_EMULATE_PREPARES(多个绑定和限制)

时间:2016-02-24 09:39:33

标签: php mysql pdo

我刚刚遇到了一个有趣的问题。我需要在参数数组中指定一个变量,但是为SQL查询中的每个出现都绑定它,我已经使用这个属性实现了这个:$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, TRUE);。但是当我使用它时,另一个带有LIMIT的查询不起作用,因为我需要指定$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);以使其正常工作(因此PDO将其处理为INT,而不是STRING)。我还尝试将 bindValue 函数与 PDO :: PARAM_INT 结合使用,但仍然没有将LIMIT值作为INT处理。

2 个答案:

答案 0 :(得分:0)

array (size=5)
  ':audit_id' => string '1' (length=1)
  ':audit_id1' => string '1' (length=1)
  ':audit_id2' => string '1' (length=1)
  ':audit_id3' => string '1' (length=1)
  ':audit_id4' => string '1' (length=1)

<强> db_query:

SELECT TYP,OSOBA,o.JMENO FROM ( (SELECT 'd.auditor_prihlasen' AS TYP, prihlasena_osoba_id AS osoba FROM dotaznik d WHERE audit_id = :audit_id AND prihlasena_osoba_id > 0) UNION (SELECT 'd.auditor_vyroba' AS TYP, auditor_vyroba_id AS osoba FROM dotaznik d WHERE audit_id = :audit_id1 AND auditor_vyroba_id > 0) UNION (SELECT 'd.auditor_kvalita' AS TYP, auditor_kvalita_id AS osoba FROM dotaznik d WHERE audit_id = :audit_id2 AND auditor_kvalita_id > 0) UNION (SELECT 'od.odpovedna_osoba' AS TYP, odpovedna_osoba_id AS osoba FROM odpoved od WHERE tema_id IN(SELECT id FROM tema WHERE audit_id = :audit_id3)) UNION (SELECT 'od.odpovedna_osoba' AS TYP, odpovedna_osoba_id_puvodni AS osoba FROM odpoved od WHERE tema_id IN(SELECT id FROM tema WHERE audit_id = :audit_id4) AND odpovedna_osoba_id_puvodni > 0) ) tbls, osoba o WHERE o.id = osoba GROUP BY typ, osoba, jmeno

我希望查询在没有audit_id1,audit_id2等的情况下运行。所以它们只使用一个变量audit_id

答案 1 :(得分:0)

  

但是当我使用它时,另一个带有LIMIT的查询并不起作用

您可以动态更改仿真模式,并在需要时将其打开和关闭。

  

我还尝试将bindValue函数与PDO :: PARAM_INT结合使用,但仍然没有将LIMIT值作为INT处理。

确保您明确地将值转换为int PDO doesn't cast them for PDO::PARAM_INT