我正在尝试使用数据库中的数据填充下拉菜单。但我有空字段没有任何数据,并且在控制台中没有错误。我错的是什么?
我将数据作为json发送并在angularjs中获取。
php中有我的DB文件。 (name = usersDB.php)
<?php
$con = mysqli_connect("localhost", "root", "password", "db");
if(!$con){
die('something wrong '.mysqli_error());
}
//echo ('test' . $_GET['action']);
switch($_GET['action']) {
case 'get_Logins_info' :
get_Logins_info();
break;
}
function get_Logins_info(){
$qry = mysqli_query('SELECT * from login');
echo("test");
$data = array();
while($rows = mysqli_fetch_array($qry))
{
$data[] = array(
"id" => $rows['id'],
"name" => $rows['name']
);
}
print_r(json_encode($data));
return json_encode($data);
}
?>
我的桌面登录如下:
id name username password
角度部分
$scope.getLogins = function() {
$http.get('forms/usersDB.php?action=get_Logins_info')
.then(function(data, status, headers, config) {
$scope.chooseLogins = data;
console.log('Retrieved data from server');
console.log(data);
})
.then(function(data, status, headers, config) {
console.log("Error in retrieving data from server");
console.log(data, status);
});
};
$scope.getLogins();
HTML
<md-select ng-model="login" >
<md-option ng-repeat="logins in chooseLogins" ng-value="logins.id">{{logins.name}}</md-option>
</md-select>
我有下拉列表但是没有。
控制台说什么?
Retrieved data from server
(index):118 Object {data: "test[]", status: 200, config: Object, statusText: "OK"}config: Objectdata: "test[]"headers: (name)status: 200statusText: "OK"__proto__: Object
(index):122 Error in retrieving data from server
(index):123 undefined undefined
一切看起来没问题但没有错误。
答案 0 :(得分:0)
问题是您没有在此行上传递数据库连接:
$qry = mysqli_query('SELECT * from login');
将其更改为:
$qry = mysqli_query($con, 'SELECT * from login');
答案 1 :(得分:0)
使用修复进行修改。
有我的php。
switch($_GET['action']) {
case 'get_Logins_info' :
get_Logins_info($con,$row);
break;
}
function get_Logins_info($con){
$qry = mysqli_query($con, "SELECT * from login");
while($row = mysqli_fetch_array($qry, MYSQLI_ASSOC))
{
$data[] = array(
"id" => $row['id'],
"name" => $row['name']
);
//printf ("%s (%s)\n", $row["id"], $row["name"]); - This print proper data
// print_r($data); - This print table in way like [0] Array [1] Proper data [2] Array [3] Proper data
}
print_r(json_encode($data)); // this print Array[2]
return json_encode($data);
}
但我仍然有空的选择字段。我认为这是我的HTML
的问题角
$scope.ChooseLogins = [];
$scope.getLogins = function() {
$http.get('forms/usersDB.php?action=get_Logins_info')
.then(function(data, status, headers, config) {
$scope.chooseLogins = data;
console.log('Retrieved data from server');
console.log(data);
})
.then(function(data, status, headers, config) {
console.log("Error in retrieving data from server");
console.log(data, status);
});
};
$scope.getLogins();
当我做了像myaddress.com/forms/usersDB.php?action=get_Logins_info这样的事情 我有来自数据库的行
html(我有两个选项但没有数据)
<md-select ng-model="newTask.login" >
<md-option ng-repeat="logins in ChooseLogins" ng-value="logins.id">{{logins.name}}</md-option>
</md-select>