无法使用LIKE绑定自定义正则表达式

时间:2018-02-12 07:27:54

标签: php mysql

使用bindParam时,我无法使用LIKE运算符创建自定义reg-ex的查询。

这是我的代码:

public function findAllByTag($tag)
    {
        $expressionOne = $tag.",%";
        $expressionTwo = "%,".$tag;
        $expressionThree = "%,".$tag.",%";
        $expressionFour = $tag;
        $query  = "SELECT * FROM Conditions WHERE  ";/*'CONCAT(:tag, ',%')*/
        $clause = "(tag LIKE :expressionOne || tag LIKE :expressionTwo || tag LIKE :expressionThree|| tag LIKE :expressionFour)";
        $query .= $clause;
        $boundParams[0] = $query;
        $stmt           = $this->getPreparedStatement($query);
        $stmt->bindParam(':expressionOne', $expressionOne, \PDO::PARAM_STR);
        $stmt->bindParam(':expressionTwo', $expressionTwo, \PDO::PARAM_STR);
        $stmt->bindParam(':expressionThree', $expressionThree, \PDO::PARAM_STR);
        $stmt->bindParam(':expressionFour', $expressionFour, \PDO::PARAM_STR);
        $boundParams[0] = $query;
        $stmt->execute($boundParams);
        $collection = new Collection();
        while ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) {
            $collection->add($this->createObject($data));
        }
        $collection->resetChanges();
        return $collection;
    }

它引发了一个错误:意外':'期待']'在下面的代码中:

class ExceptionResponse extends Response
{
    /**
     * It prepares response message from message and code of exception, statusCode is initialized to exception code and
     * message is an array of code and message of format {_errors: [ {'code' : int, 'message': string }, .. ]}
     *
     * @param Exception $e exception from which return response will be created
     */
    public function __construct(\Exception $e)
    {
        if (!($e instanceof GenericException)) {
            $e = new GenericException($e->getMessage());
        }
        $data = [
            'error' => true,
            'failure' => [
                [
                    'code'    => $e->getCode(),
                    'message' => $e->getMessage(),
                ],
            ],
            'success': null
        ];
        $this->setHeader('Content-Type', 'application/json');
        $this->setMessage(json_encode($data));
        $this->setStatusCode($e->getCode());
    }
}

第39行:'success': null

如何删除此错误并使我的查询有效? 注意:我正在使用我公司的框架,格里芬并在其上编写代码。 一切正常,但bindParam。

1 个答案:

答案 0 :(得分:0)

我尝试使用bindValues,它运行得很好。

        $expressionOne = $tag.",%";
        $expressionTwo = "%,".$tag;
        $expressionThree = "%,".$tag.",%";
        $expressionFour = $tag;
        $clause = "(tag LIKE :expressionOne || tag LIKE :expressionTwo || tag LIKE :expressionThree|| tag LIKE :expressionFour)";
        $query  = "SELECT * FROM Conditions WHERE ";
        //$clause = "(tag LIKE :expressionOne)";
        $query .= $clause;
        $boundParams[0] = $query;
        $stmt           = $this->getPreparedStatement($query);
        //$stmt->bind_param("s", $tag);
        $stmt->bindValue(':expressionOne', $expressionOne, \PDO::PARAM_STR);
        $stmt->bindValue(':expressionTwo', $expressionTwo, \PDO::PARAM_STR);
        $stmt->bindValue(':expressionThree', $expressionThree, \PDO::PARAM_STR);
        $stmt->bindValue(':expressionFour', $expressionFour, \PDO::PARAM_STR);