我需要做一个简单的查询
$array_of_ids = array();
//poulate $array_of_ids, they don't come from another db but from Facebook
//so i can't use a subquery for the IN clause
$wpdb->prepare("SELECT id from table where id IN (%d, %d)", $array_of_ids [0], $array_of_ids [1]);
问题是,如果我的数组中有 200 个元素,处理这个问题的正确方法是什么?我是否必须手动构建包含 200 个元素的查询%d
?我需要这个查询,因为我必须将我的数据库与 facebook 数据“同步”,并且我必须检查数据库中的用户是否存在,更新存在的用户,插入新用户并删除不是我朋友的用户。
如果您确定数组元素是数字:
$wpdb->prepare("SELECT id FROM table WHERE id IN ("
. implode(',',$array_of_ids) . ")");
否则,您可以使用vsprintf
的形式prepare
传入参数数组:
$wpdb->prepare("SELECT id FROM table WHERE id IN ("
. str_repeat("%d,", count($array_of_ids)-1) . "%d)" , $array_of_ids);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)