我在这里使用这个程序GameQ v3,我有一个名为servers的数据库表。字段是id,game,ip,port和query_port。我想要做的是回显表中的所有服务器和信息,就像我已设法从数据库获取信息到下面的代码。一切正常,直到我将2服务器添加到列表然后它给我一个错误
未捕捉GameQ \例外\服务器:缺少服务器信息密钥'类型'! GameQ \ Server-> __ construct(数组)#1 GameQ \ GameQ-> addServer(数组)#2 {main}抛出
我不确定我做错了什么。但如果我在数据库中只有一台服务器,它就可以工作。
$db = dbconnect();
$stmt = $db->prepare("SELECT * FROM servers");
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
if ($result->num_rows == 1) {
$row = $result->fetch_array();
$ID = $row['id'];
$GAME = $row['game'];
$IP = $row['ip'];
$PORT = $row['port'];
$QUERYPORT = $row['query_port'];
}
$GameQ = new \GameQ\GameQ();
$GameQ->setOption('timeout', 5); // seconds
$GameQ->addFilter ( 'normalize' );
$GameQ->addServer([
'id' => $ID,
'type' => $GAME,
'host' => $IP.':'.$PORT,
'options' => [
'query_port' => $QUERYPORT,
],
]);
$results = $GameQ->process();
print_r($results);
echo "<pre>";
print_r ( $results );
echo "</pre>";
答案 0 :(得分:1)
1. if
块之后的代码需要在if
内(解决变量范围问题)
2.A while
循环也需要返回所有记录。
$db = dbconnect();
$stmt = $db->prepare("SELECT * FROM servers");
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
if ($result->num_rows > 0) {
while($row = $result->fetch_array()){
$ID = $row['id'];
$GAME = $row['game'];
$IP = $row['ip'];
$PORT = $row['port'];
$QUERYPORT = $row['query_port'];
$GameQ = new \GameQ\GameQ();
$GameQ->setOption('timeout', 5); // seconds
$GameQ->addFilter ( 'normalize' );
$GameQ->addServer([
'id' => $ID,
'type' => $GAME,
'host' => $IP.':'.$PORT,
'options' => [
'query_port' => $QUERYPORT,
],
]);
$results = $GameQ->process();
echo "<pre/>";print_r($results);
}
}