gpt4 book ai didi

php - 如何在 mysql 查询的 'IN' 子句中使用来自 PHP 的值数组?

转载 作者:行者123 更新时间:2023-11-29 01:33:10 25 4
gpt4 key购买 nike

//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 中的数据库中检索 10 个最佳得分手,但在我的第二个查询中,我猜数组名称 $friend_pics 不起作用,请帮助我,谢谢

最佳答案

没有对此的原生支持。即使绑定(bind)参数 API 也不允许将数组用于 IN 子句。您必须使用一些辅助代码构建查询:

$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()然而:

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

关于php - 如何在 mysql 查询的 'IN' 子句中使用来自 PHP 的值数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5207699/

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