Codi
Codi

Reputation: 438

php array only allows 1 id of information return

I am using this program here GameQ v3 and I have a database table called servers. the fields are id, game, ip, port and query_port. what I am trying to do is echo all servers and info in a table like a list I have managed to get the information from the database to the code below. every thing works fine until I add a 2 server to the list then it gives me an error

Uncaught GameQ\Exception\Server: Missing server info key 'type'! GameQ\Server->__construct(Array) #1 GameQ\GameQ->addServer(Array) #2 {main} thrown

I'm not sure what I am doing incorrectly. but it works if I only have 1 server in the database.

                $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>";  

Upvotes: 2

Views: 88

Answers (1)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

1.Code after if block need to be inside if (to resolve variable-scope problem)

2.A while loop needed too for returning all records.

$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);
  }
}

Upvotes: 1

Related Questions