I am trying to fill dropdown menu with my data from database. But I have empty fields without any data and without errors in console. What am I droing wrong?
I am sending data as json and get it in angularjs.
There is my DB file in php. ( 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);
}
?>
My table login looks like:
id name username password
Angular part
$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>
I have dropdown list but is empty.
What console say?
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
Everything looks ok no errors but is empty.
The problem is that you aren't passing the database connection on this line:
$qry = mysqli_query('SELECT * from login');
Change it to:
$qry = mysqli_query($con, 'SELECT * from login');
Edit with fix.
There is my 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)
", $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);
}
But I still have empty select fields. I think that is problem with my html
Angular
$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();
When I did something like myaddress.com/forms/usersDB.php?action=get_Logins_info I have my rows from database
html ( I have two options but without data )
<md-select ng-model="newTask.login" >
<md-option ng-repeat="logins in ChooseLogins" ng-value="logins.id">{{logins.name}}</md-option>
</md-select>