如何在MySQL查询的'IN'子句中使用PHP的值数组? [英] How to use an array of values from PHP in the 'IN' clause of mysql query?

查看:116
本文介绍了如何在MySQL查询的'IN'子句中使用PHP的值数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//get all id's of ur friend that has installed your application
 $friend_pics=$facebook->api( array( 'method' => 'fql.query', 'query' => "SELECT uid FROM user WHERE uid   IN(SELECT uid2 from friend WHERE uid1='$user') AND is_app_user = 1" ) ); // this query work fine
//your top10 friends in app
$result="SELECT * FROM fb_user WHERE user_id IN($friend_pics) ORDER BY oldscore DESC LIMIT 0,10";
db_execute($result);

我想从存储在oldscore中的数据库中检索十个得分最高的得分手,但是在我的第二个查询中,我猜数组名称$ friend_pics无法正常工作,请帮我谢谢

i want to retrive ten top scorer from my database stored in oldscore but in my second query the array name $friend_pics is not working i guess,plz help me thanks

推荐答案

对此没有本地支持.甚至绑定参数API也不允许对IN子句使用数组.您必须使用一些辅助代码来构造查询:

There is no native support for that. Even the bound parameter APIs do not allow to use arrays for IN clauses. You have to construct the query with some helper code:

$friend_pics = array_map("mysql_real_escape_string", $friend_pics);
$friend_pics = "'" . implode("', '", $friend_pics) . "'";
"SELECT * WHERE user_id IN ($friend_pics)              "

一个更简单的选择是mysqls FIND_IN_SET() 但是:

A simpler alternative would be mysqls FIND_IN_SET() however:

$friend_pics = mysql_real_escape_string(implode(",", $friend_pics));
"SELECT * FROM fb_user WHERE find_in_set(user_id,'$friend_pics')     "

这篇关于如何在MySQL查询的'IN'子句中使用PHP的值数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆