将数组发送到PHP Web服务并从MySQL数据库返回数组

时间:2017-06-14 12:24:36

标签: javascript php mysql angularjs arrays

我正在使用MySQL作为后端数据库在AngularJS中开发一个应用程序。 Web服务使用PHP 5.6.30。

我想知道从MySQL数据库的表User中检索通过itemsArray数组发送到Web服务的每个用户名的信息列表的最佳方法是什么:

  • itemsArray数组与用户名列表一起发送;
  • 从Web服务返回returnedArray数组,其中包含一系列信息。对于每个用户名,将返回idUser,用户名和密码以显示在HTML页面中。

MySQL数据库中User表的结构:

  • idUser(int)
  • 用户名(varchar)
  • 密码(varchar)

发送到Web服务的数据结构itemsArray(Array)的示例:

var itemsArray = ["admin1", "admin2", "admin3", "admin4", "admin5"];

用于显示Web服务返回的数据的HTML示例:

<ul><li ng-repeat="item in returnedArray track by $index">{{ item }}</li></ul>

控制器样本:

angular.module('myApp').controller('Ctrl1', ['$scope', 'services', Ctrl1]);

function Ctrl1($scope, services) {
    var itemsArray = ["admin1", "admin2", "admin3", "admin4", "admin5"];
    //request to MySQL BD
    services.getInfo(itemsArray).then(function(data){
      $scope.returnedArray = data.data;
    });
  }

向Web服务发送请求的app.js示例:

.factory('services', ['$http', function($http){
  var serviceBase = 'services/';
  var obj = {};
  obj.getInfo = function (arrayItems) {
    return $http.get(serviceBase + 'get_info?arrayItems=' + arrayItems);
  };
  // return obj
  return obj;
}]);

PHP中的Web服务示例:(必须完成)

// GET - Get Info
  private function get_info(){
    if($this->get_request_method() != "GET"){
      $this->response('', 406);
    }
    $arrayItems = (array)$this->_request['arrayItems'];
    $query = "TO COMPLETE";
    $r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
    if($r->num_rows > 0){
      $result = array();
      while($row = $r->fetch_assoc()){
        $result[] = $row;
      }
      $this->response($this->json($result), 200); // send user details
    }
    $this->response('', 204);   // send user detail
  }

谢谢*

3 个答案:

答案 0 :(得分:0)

你不应该只需要:

$this->response(json_encode($result), 200);

而不是:

$this->response($this->json($result), 200);

答案 1 :(得分:0)

可能是这样的:

private function get_info()
{
    if ($this->get_request_method() != 'GET') {
        $this->response('', 406);
    }
    $arrayItems = (array) $this->_request['arrayItems'];
    $query = 'SELECT * FROM `table` WHERE `field` = :key';
    $r = $this->mysqli->prepare($query);
    // as an example
    $this->mysqli->bind_param('key', current($arrayItems));
    $this->mysqli->execute();

    $result = array(); 
    $responseCode = 204;
    while ($row = $r->fetch_assoc()) {
        $responseCode = 200;
        $result[] = $row;
    }
    // send user details
    $this->response($this->json($result), $responseCode); 
}

答案 2 :(得分:0)

同时,我解决了自己的问题。

如果有人在将来需要它,有解决方案:

(初始问题:将JavaScript字符串数组转换为PHP字符串数组)

解决方案:

 private function get_info(){
    if($this->get_request_method() != "GET"){
      $this->response('', 406);
    }
    $arrayItems = $this->_request['arrayItems'];

    //Explode on comma
    $vals = explode(',', $arrayItems);
    //Trim whitespace
    foreach($vals as $key => $val) {
      $vals[$key] = trim($val);
    }
    //Return empty array if no items found
    //http://php.net/manual/en/function.explode.php#114273
    $arrayItems_php = array_diff($vals, array(""));

    $usernames = implode("','", $arrayItems_php);
    $query = "SELECT idUser, Username, Password FROM User
    WHERE Username IN ('$usernames')";
    $r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);


   if($r->num_rows > 0){
      $result = array();
      while($row = $r->fetch_assoc()){
        $result[] = $row;
      }
      $this->response($this->json($result), 200); // send user details
    }
    $this->response('', 204);   // send user detail
  }