首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel 5自定义查询参数编号无效

Laravel 5自定义查询参数编号无效
EN

Stack Overflow用户
提问于 2017-07-11 06:58:31
回答 2查看 1.1K关注 0票数 0

在Laravel 5中,我正在尝试进行客户查询。我的代码是这样的:

$params =数组( 'criteria‘=> $criteria,'criteria1’=> $criteria );

代码语言:javascript
复制
    //Define the SQL
    $sql = 'SELECT * FROM ' . $this -> _taskTableName .' 
    JOIN ' . $this -> _userTableName .' ON 
    ' . $this -> _userTableName .'.id = ' . $this -> _taskTableName .' .client_id 
    WHERE notes LIKE \'%:criteria%\' OR name LIKE \'%:criteria1%\' ';


    //Exeute the search
    $tasks = DB::statement(DB::raw($sql),$params);

除非我一直收到这个错误,即使我删除了DB::raw,并且我也尝试了DB::select。

代码语言:javascript
复制
SQLSTATE[HY093]: Invalid parameter number: :criteria (SQL: SELECT * FROM tasks 
JOIN users ON 
users.id = tasks .client_id 
WHERE notes LIKE '%:criteria%' OR name LIKE '%:criteria1 %' )
in Connection.php (line 647)
at Connection->runQueryCallback(object(Expression), array('criteria' => 'Devin', 'criteria1' => 'Devin'), object(Closure))
in Connection.php (line 607)
at Connection->run(object(Expression), array('criteria' => 'Devin', 'criteria1' => 'Devin'), object(Closure))
in Connection.php (line 450)

有谁知道为什么会发生这种情况,以及如何修复?

EN

回答 2

Stack Overflow用户

发布于 2017-07-11 07:13:53

尝试使用带有$params数组的DB:select()作为第二个参数,如下所示:

代码语言:javascript
复制
$params = array( 'criteria' => $criteria, 'criteria1' => $criteria );

//Define the SQL
$sql = 'SELECT * FROM ' . $this -> _taskTableName .' 
JOIN ' . $this -> _userTableName .' ON 
' . $this -> _userTableName .'.id = ' . $this -> _taskTableName .' .client_id 
WHERE notes LIKE \'%:criteria%\' OR name LIKE \'%:criteria1%\' ';


//Exeute the search
$tasks = DB::select($sql, $params);
票数 0
EN

Stack Overflow用户

发布于 2017-07-11 07:19:57

你可以像下面这样做

代码语言:javascript
复制
$sql = 'SELECT * FROM ' . $this -> _taskTableName .' 
        JOIN ' . $this -> _userTableName .' ON 
        ' . $this -> _userTableName .'.id = ' . $this -> _taskTableName .' .client_id 
        WHERE notes LIKE \'%?%\' OR name LIKE \'%?%\' ';
    DB::select($sql,array($criteria,$criteria));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45022900

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档