gpt4 book ai didi

mysql - Laravel:在查询中使用 WHERE CONCAT

转载 作者:行者123 更新时间:2023-11-29 02:47:14 26 4
gpt4 key购买 nike

我是 laravel 的新手,遇到了使用 CONCAT 构建查询的问题:

#From input
$password = $request->password;
#sql statement
UserMainTbl::where('username', '=', $username)->whereRaw('hashkey', '=', CONCAT('admin_id'.$password))

表:UserMainTbl
字段:用户名、hashkey、admin_id

出现错误:

Call to undefined function App\Http\Controllers\Auth\CONCAT()

------
更新:
我更改了我的代码并设法阻止了上述错误。但是出现新的错误。

->where('hashkey', '=', DB::raw('concat(admin_id,"$password")'))

Column not found: 1054 Unknown column 'password123' in 'where clause' (SQL: select * from user_main_tbl where username = xxx and hashkey = concat(admin_id,password123) limit 1)

------
更新[求解]:
我对这个不好。它只是一个简单的字符串。这是供将来引用的解决方案,如果有的话。哈哈:

->where('hashkey', '=', DB::raw('concat(admin_id,"'.$password.'")'))


有人可以帮助指出我该怎么做吗?
非常感谢。

最佳答案

我知道您已经解决了这个问题,但是如果您不转义用户输入,您的解决方案可能会为 SQL 注入(inject)打开可能性。解决此问题的一种方法是添加绑定(bind)。

UserMainTbl::where('username', '=', $username)
->where('hashkey', '=',DB::raw('concat(admin_id,"?")'))
->addBinding($password);

https://laravel.com/docs/5.3/queries#raw-expressions

关于mysql - Laravel:在查询中使用 WHERE CONCAT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40414316/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com