How can i get multiple table rows from the MySQL database into the application by a JSON object.
Getting one row from the table works well for me! But i cant get multiple rows, it throws an error saying "SyntaxError: Unexpected token { in JSON at position..."
How do i achieve this please help me!!
This is my angular Code..(here im passing a variable and get the matching rows and get them to the console.)
$scope.companySelected = function(emp_Comp){
console.log('Selected Item is : '+ emp_Comp);
$http({
method: 'GET',
url: 'http://localhost/companyPopulate.php',
params: {employeeCompany : emp_Comp}
}).then(function successCallback(response) {
$scope.data = response.records;
console.log('success :'+ JSON.stringify(response));
//console.log(testdata.data);
}, function errorCallback(response) {
//console.log('error',response);
// called asynchronously if an error occurs
// or server returns response with an error status.
});
}
This is my PhP Script...
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
//http://stackoverflow.com/questions/18382740/cors-not-working-php
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
include_once 'config.php';
//create connection
$conn = new mysqli($mysql_host, $mysql_user, $mysql_password,$mysql_database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$fields = array();
if(isset($_GET['employeeNo']))
$fields['employeeNo'] = $_GET['employeeNo'];
if(isset($_GET['employeeName']))
$fields['employeeName'] = $_GET['employeeName'];
if(isset($_GET['employeeCompany']))
$fields['employeeCompany'] = $_GET['employeeCompany'];
$sql = "SELECT employeeName,employeeNo FROM employee_info WHERE ";
$count = 0;
foreach($fields as $key => $value){
if($count == 0)
$sql .= $key . "='" . $value . "' ";
else
$sql .= "AND " . $key . "='" . $value . "' ";
$count++;
}
$fields = array();
$result = $conn->query($sql);
$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC))
{
if ($outp != "")
{
$outp .= ",";
}
$outp .= '{"employeeName":"' . $rs["employeeName"] . '"}';
}
$outp ='{"records":['.$outp.']}';
$conn->close();
echo $outp;
/* }else{
echo 'parameters expected from the client are missing! :) ';
} */
?>
I'm getting the correct results when I'm running the script alone in the browser.
Please Help me!! Thanks in Advance!!