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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料