【问题标题】:PHP PDO Prepared Statements with Where IN Clause带有 Where IN 子句的 PHP PDO 准备语句
【发布时间】:2017-06-12 08:29:54
【问题描述】:

这是我的 PHP PDO 代码

$stmt = $conn->prepare("SELECT * FROM TABLE
WHERE tag1 IN ('$tag1','$tag2') $andor tag2 IN ('$tag1','$tag2 ') ORDER BY $sort DESC LIMIT $limit OFFSET $start");
     // Then fire it up
     $stmt->execute();
     // Pick up the result as an array
     $result = $stmt->fetchAll();
    // Now you run through this array in many ways, for example

我正在尝试将其转换为准备好的语句,但我真的不明白它是如何工作的。我从 Google 尝试了很多东西,但都没有奏效。

【问题讨论】:

  • 你可以从What prepared statements are开始。
  • IN ('$tag1','$tag2') $andor$andor 的值是多少,这看起来不像是正确的 sql
  • $andor 是列吗?
  • 否,如果标签 2 为空,$andor = OR,如果标签 2 不为空,$andor = AND。抱歉回复晚了,我得做一些紧急工作

标签: php pdo prepared-statement


【解决方案1】:
$stmt = $conn->prepare("SELECT * FROM table WHERE tag1=? OR tag1=? AND tag2=? OR tag2=? ORDER BY id DESC LIMIT 15,10");

$stmt->execute(array($tag1, $tag2, $tag1, $tag2));

$result = $stmt->fetch(PDO::FETCH_ASSOC);

我希望它有效。

【讨论】:

  • 致命错误:未捕获的 PDOException:SQLSTATE[42S22]:未找到列:1054 C:\xampp\htdocs\website\index.php:189 中的“where 子句”中的未知列“City”跟踪:#0 C:\xampp\htdocs\website\index.php(189): PDOStatement->execute(Array) #1 {main} 在 C:\xampp\htdocs\website\index.php 第 189 行抛出
  • @Josh 你能参加我上面的 cmets
猜你喜欢
  • 2013-04-21
  • 2016-12-16
  • 2016-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多