【问题标题】:PHP / PDO : SQl to find row which contains a string value [closed]PHP / PDO:SQl查找包含字符串值的行[关闭]
【发布时间】:2014-08-04 02:31:00
【问题描述】:

我尝试请求查找包含特定字符串值的行。

这是我的代码摘录:

// Getting motscles value
        $motscles = $_POST['motscles'];

        // Prepare a second SQL request to get all annonces posted by the user
        $result2=$connexion->prepare("select * from annonces where titre LIKE = '%".$motscles."%' ");

我没有结果,我认为我的要求很糟糕..

【问题讨论】:

  • 去掉LIKE关键字后面的等号;这是无效的语法。
  • 哦,您目前对SQL Injection 开放。你似乎有能力使用prepared statements,所以这样做,否则后果自负。

标签: php mysql sql pdo


【解决方案1】:

另外@abhik's回答如果准备得当的话

$stmt=$connexion->prepare('select * from annonces where titre LIKE ?');
$result2=$stmt->execute(array('%'.$motscles.'%'));

【讨论】:

    【解决方案2】:
    LIKE =
    

    这不对,应该是-

    LIKE  '%".$motscles."%'
    

    在这里查看like通配符用法

    http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like

    【讨论】:

      猜你喜欢
      • 2012-08-01
      • 2023-02-25
      • 2021-07-19
      • 2019-03-16
      • 2017-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-04
      相关资源
      最近更新 更多