douzao2590 2015-10-08 10:42
浏览 89

使用PHP和angular.js将值返回到错误回调函数

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.

  • 写回答

1条回答

  • dongqian2021 2015-10-08 12:17
    关注

    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);
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试