2017-06-14 102 views
0

我正在用MySQL作爲後端數據庫在AngularJS中開發一個應用程序。 Web服務使用PHP 5.6.30。將數組發送到PHP Web服務並從MySQL數據庫返回數組

我想知道什麼是從我的MySQL數據庫的表User檢索通過itemsArray陣列發送到Web服務的每個用戶名的信息列表的最佳方式:

  • itemsArray數組與用戶名列表一起發送;
  • returnedArray數組是從Web服務返回的信息列表。對於每個用戶名,idUser,用戶名和密碼被返回以顯示在HTML頁面中。

從MySQL數據庫的User表的結構:

  • ID用戶(INT)
  • 用戶名(爲varchar)
  • 密碼(VARCHAR)

樣品的數據結構itemsArray(Array)tha t被髮送到Web服務:

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

樣品的HTML來顯示由Web服務返回的數據的:

<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; 
    }); 
    } 

樣品的app.js發送請求到Web服務:

.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 
    } 

謝謝*

+0

有什麼問題嗎?..你有沒有嘗試過從數據庫中獲取值?只有兩種方式......'fetchAll'和'fetch'(最後一種適用於大範圍) – FieryCat

+0

顯示用戶密碼 - 你在開玩笑嗎?您的應用程序不應該首先存儲實際的用戶密碼。 – CBroe

+0

@CBroe:我創建了Users表作爲測試=這是虛構的數據。我不會在我的應用程序中實際顯示存儲的用戶和密碼。我的應用程序不存儲這種信息。 – Julia

回答

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 
    } 
0

你不應該只需要:

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

代替:

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

它可以是框架邏輯的一部分......不應該是這樣 – FieryCat

0

它可能是某人hing那樣:

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); 
}