Php while 数组中的循环错误,连接到 Postgresql 数据库
我有一个静态代码来更新一些选择框。
原始结构代码:
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请不要尝试自己构建 JSON 数据。PHP 有一个函数:
json_encode()
Please do not try to build JSON data yourself.. PHP has a function for it:
json_encode()