gpt4 book ai didi

javascript - 从数据库中提取数据并在 Angular 中显示

转载 作者:搜寻专家 更新时间:2023-10-30 23:44:13 27 4
gpt4 key购买 nike

我是 Angular 的新手,正在让它与 PHP 一起工作,所以请多多包涵。我成功地用 php 将数据保存到数据库中。现在我想检索它并将其显示在列表中。数据从数据库返回,但未显示在我的 View 中 (html)

我的 html 代码是:

<!-- Show users from db -->
<div>
<ul ng-init="get_user()">
<li ng-repeat="user in userInfo track by $index">{{user.user_name}} + {{user.user_email}}</li>
</ul>
</div>
<!-- Show users from db -->

我的 app.js 的代码是这样的:

/** function to get info of user added in mysql referencing php **/
$scope.get_user = function() {
$http.get('db.php?action=get_user').success(function(data)
{
$scope.userInfo = data;
console.log("You were succesfull in show user info");
console.log(data);
})
}

最后是我的 php 代码:

/**  Function to get a user   **/
function get_user() {
$qryGet = mysql_query('SELECT * from tblUser');

echo ($qryGet);

$data = array();
while($rows = mysql_fetch_array($qryGet))
{
$data[] = array(
"id" => $rows['id'],
"user_name" => $rows['user_name'],
"user_email" => $rows['user_email']
);
}

print_r(json_encode($data));

return json_encode($data);
}

在浏览器中运行它时,我会在控制台中看到它。 (http://gyazo.com/4621faa287a041ed9f7f610552407e9f)。欢迎任何线索

最佳答案

那是因为你的数据不是

[{"id":"1", "user_name":"Foo Bar" ... }]

它是:

Connected to the DBResource id #4[{"id":"1", "user_name":"Foo Bar" ... }]

您可以在控制台日志中看到它。删除显示

的行
echo ($qrtGet);

在你的 php 中

关于javascript - 从数据库中提取数据并在 Angular 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31029690/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com