我想循环遍历MySQL结果,这是一个Freebase MID数组,我希望输出是Freebase文章的名称!
这是我的代码:
$query = "SELECT `mid` FROM `items`";
$result = mysql_query($query);
$count = 1;
while ($row = mysql_fetch_array($result)) {
$mid = $row['mid'];
$simple_query = array('name'=> null, 'mid'=>$mid);
$q_array = array('q'.$count=>array('query'=>$simple_query));
array_push ($query_array, $q_array );
$count++;
}
$jsonquerystr = json_encode($query_array);
$apiendpoint = "http://api.freebase.com/api/service/mqlread?queries";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$apiendpoint=$jsonquerystr");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonresultstr = curl_exec($ch);
curl_close($ch);
$resultarray = json_decode($jsonresultstr, true); #true:give us the json struct as an array
foreach($resultarray as $name){
echo "$name<br>";
}
错误:
{“code”:“/ api / status / error”,“messages”:[{“code”:“/ api / status / error”,“message”:“'list'对象没有属性'get '“}],”status“:”500 Internal Error“,”transaction_id“:”cache; cache04.p01.sjc1:8101; 2012-05-14T07:31:39Z; 0079“}
答案 0 :(得分:2)
您的$query_array
应该是按名称而不是关联数组数组的关联查询数组。所以不要这样:
$q_array = array('q'.$count=>array('query'=>$simple_query));
array_push ($query_array, $q_array );
..看起来应该是这样的:
$q_array = array('query'=>$simple_query);
$query_array['q'.$count] = $q_array;
但是,该代码使用的是即将弃用的旧Freebase API。在new API中执行此操作的更好方法是将其全部构造为一个查询,如下所示:
[{
"mid": null,
"name": null,
"topics:mid|=":[
"/m/045c7b",
"/m/0d6lp",
"/m/021ympy",
...
]
}]
这允许您传入MID数组并为每个MID返回一个名称。从新API获取此URL的URL应如下所示:
https://www.googleapis.com/freebase/v1/mqlread/?query=[{...}]