数组中Php while Loop错误,连接到Postgresql数据库


Php while Loop error in a Array, connecting to Postgresql Database

我有一个静态代码来更新一些选择框。

原始结构代码:

$result = "
      ({ 
        'options': [
          {'value': '','description': '(pick an item)'}, 
          {'value': 'test','description': 'test'}, 
          {'value': 'test_2','description': 'test_2'}
        ]
      });
    ";

我正试图将其动态化,并连接到Postgresql数据库中的表,但我的代码不起作用。

这是我的代码:

if($value=="asus"){
    require("includes/connection.php");
    $sth = $dbh->prepare( "SELECT * FROM mapa_ferias WHERE area = 'Asus' " );
    $sth->setFetchMode(PDO::FETCH_ASSOC);
    $sth->execute();
    $result = "
    ({
      'options': [";
            while($row = $sth->fetch()) { 
            $result += "{'value': '" + $row['nome'] + "','description': '" + $row['nome'] + "'},";
    }
    $result += 
            "]
        });
    ";
  }

我希望你能帮我。提前谢谢。

请不要尝试自己构建JSON数据。。PHP有一个函数:json_encode()

if ($value=="asus"){
    require("includes/connection.php");
    $sth = $dbh->prepare( "SELECT * FROM mapa_ferias WHERE area = 'Asus' " );
    $sth->setFetchMode(PDO::FETCH_ASSOC);
    $sth->execute();
    $options = array();
    while($row = $sth->fetch()) { 
        $options[] = array(
            'value' => $row['nome'], 
            'description' => $row['nome']
        );
    }
    $sth->closeCursor();
    $result = '('.json_encode(array(
        'options' => $options
    )).')';
}