将php数组传递到sql查询 [英] passing a php array into an sql query
本文介绍了将php数组传递到sql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试对与php数组中的值匹配的行进行sql查询.
I'm trying to make sql query for rows matching values in a php array.
基本上我有一个像这样的数组
Essentially I have an array like
$userIDs[0] = 23456;
$userIDs[1] = 42901;
$userIDs[2] = 82731;
$userIDs[3] = 23921;
并希望执行单个SQL查询以获取与该数组匹配的行
And want to perform a single SQL query to get rows matching this array
SELECT * FROM users WHERE userID IN $userIDs
有没有简单的方法可以做到这一点?还是我必须手动构造查询字符串?
Is there an easy way to do this? Or do I have to manually construct the query string?
推荐答案
使用内置函数即
$QueryStr = "SELECT * FROM users WHERE userID IN (".implode(',', $userIDs).")";
这篇关于将php数组传递到sql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文