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

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决