Php while 数组中的循环错误,连接到 Postgresql 数据库

发布于 2024-11-17 16:51:44 字数 879 浏览 3 评论 0原文

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

原始结构代码:

$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 += 
            "]
        });
    ";
  }

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

I have a static code to update some select boxes.

The original structure code:

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

I'm trying to make this dynamic and connect to my table in Postgresql Database but my code don't works.

Here is My Code:

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 += 
            "]
        });
    ";
  }

I hope you can help me. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不即不离 2024-11-24 16:51:44

请不要尝试自己构建 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
    )).')';
}

Please do not try to build JSON data yourself.. PHP has a function for it: 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
    )).')';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文