使用以下代码,我使用FQL获取我在Facebook上管理的所有页面。我不打印它们。
$PageNames = $facebook->api('/fql', array('q' =>
'SELECT name, page_id FROM page WHERE page_id IN (
SELECT page_id FROM page_admin WHERE uid=me() )'));
以前,我在我的数据库中添加了一些页面的* page_id *。我的目标是显示我尚未添加到我的数据库的页面。它工作正常,代码如下。
foreach($PageNames['data'] as $PageName) {
$investigate_id = mysql_query("SELECT page_id FROM pages WHERE page_id='"
.$PageName['page_id']."' LIMIT 1 ");
if(mysql_num_rows($investigate_id) == 0) {
echo $PageName['page_id'].$PageName['name'];
}
}
我的问题/问题是,如果我能以某种方式避免大量的数据库调用,因为它会对我管理的每个页面进行查询。我怎样才能做到这一点?
答案 0 :(得分:2)
<?php
$IDs = array();
foreach($PageNames['data'] as $PageName){
$IDs[] = $PageName['page_id'];
}
$investigate_id = mysql_query('SELECT page_id FROM pages WHERE page_id IN (\''.implode('\', \'', $IDs)).' LIMIT 1 ');
while($assoc = mysql_fetch_assoc($investigate_id)){
// iff
}
?>
使用WHERE col IN将使一个查询全部为ID
WHERE page_id IN(1,3,45,6,7,6,7,5,2,1,5,76)它将在查询中执行,只需在结果之后进行循环并执行任何操作你想要的!
答案 1 :(得分:1)
您可以从使用FQL获得的结果中收集ID,然后在DB中查询结果中存在的行,以便从结果中过滤DB中存在的行...
$fql = <<<FQL
SELECT name, page_id FROM page WHERE page_id IN (
SELECT page_id FROM page_admin WHERE uid = me()
)
FQL;
// Get results from API
$pages = $facebook->api('/fql', array('q' => $fql));
// Collect pages for later usage by ID
$pagesByIds = array();
foreach($pages['data'] as $page){
$pagesByIds[$page['page_id']] = $page;
}
// Query DB for all pages that exists in results
$pagesIds = implode(',', array_keys($pageByIds));
$res = mysql_query("SELECT page_id FROM pages WHERE page_id IN ({$pageIds})");
while($pageRow = mysql_fetch_assoc($res)){
$pageId = $pageRow['page_id'];
// Remove pages that present in API results and DB
if (isset($pagesById[$pageId])) unset($pagesById[$pageId]); ;
}
// Display details for pages not existing in DB
foreach ($pagesByIds as $page){
echo "ID: {$page['page_id']}, Name: {$page['name']} \n"
}