I need to return value to error call back function when the sql query will not execute using PHP and angular.js.I am explaining my code below.
session.php:
<?php
session_start();
$connect = mysqli_connect("localhost", "root", "Oditek123@", "******");
$result = mysqli_query($connect, "SELECT * FROM db_Admin_Master WHERE id=". $_SESSION["admin_id"]);
$data = array();
while ($row =mysqli_fetch_assoc($result)) {
$data[] = $row;
}
print json_encode($data);
?>
dashboardController.js:
var dashboard=angular.module('Channabasavashwara');
dashboard.controller('dashboardController',function($scope,$http){
$http({
method: 'GET',
url: 'php/Login/session.php',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).then(function successCallback(response){
//console.log('session',response);
$scope.userType=response.data[0].user_name;
},function errorCallback(response) {
//console.log('session',response);
});
})
Here my requirement is when the query will execute the variable $data
will return to success callback function and when it will not execute due to any wrong id some message will return to error call back function.Please help me.
The $http
response
callback parameter expects a server status code, not an SQL error or any other kind of message. So by all means, if you get a query error (or even a successful query with no results), you're going to receive success if the promise of the HTTP request is fulfilled, but the response will be the SQL error message (or code), if ever handled.
You have to check for query issues inside PHP and handle it there. Then, if you want, you can send headers with a new HTTP status response, or just raw message text.
Ex:
<?php
session_start();
$connect = mysqli_connect("localhost", "root", "Oditek123@", "******");
$result = mysqli_query($connect, "SELECT * FROM db_Admin_Master WHERE id=". $_SESSION["admin_id"]);
$data = array();
if ($result) { // if everything goes ok
while ($row =mysqli_fetch_assoc($result)) {
$data[] = $row;
}
} else { // else, send error
$data['errno'] = mysqli_errno($connect);
$data['error'] = mysqli_error($connect);
}
print json_encode($data);
?>