如何将外部数组传递给查询?
我是 mysql 查询的新手,我有一个查询将搜索数组 status in ('php','laravel','apiato')
中的所有项目,如果我这样使用它就可以工作,现在我必须向数组添加更多值,而不是传递字符串,我从该文件创建了一个常量文件,我想发送一个数组,它抛出一个异常数组到字符串转换
public function getALlData(){
$array = BooksConstants::Status_constants;
DB::select("SELECT count(1) as total_transactions from books where books.account_id in ('$this->account_ids') and DATE(created_at) between ? and ? and status in $array ", [$fromDate, $toDate]);
}
I am new to the mysql queries, i have one query which will search all the items inside the array status in ('php','laravel','apiato')
if i use like this it's working, now i have to add more values to the array instead of passing strings i made one constant file from that file i want to send an array,it's throwing an exception Array To string Conversion
public function getALlData(){
$array = BooksConstants::Status_constants;
DB::select("SELECT count(1) as total_transactions from books where books.account_id in ('$this->account_ids') and DATE(created_at) between ? and ? and status in $array ", [$fromDate, $toDate]);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我们使用
implode()
将数组转换为字符串,然后在array_map()
函数内使用trim()
函数删除空格。array_map()
函数将数组的每个值发送到用户创建的函数。First we use
implode()
to convert array to string then insidearray_map()
function usetrim()
function to remove whitespaces. Thearray_map()
function sends each value of an array to a user-made function.