使用where子句使用angularjs从数据库中获取数据

时间:2016-11-10 12:49:53

标签: php mysql angularjs

我想使用angularjs从mysql数据库加载数据。

这是应用程序的工作方式; 用户登录并将其用户名存储在cookie中。 该用户名显示在主页上

我想获取此值并通过angularjs传递给php脚本,以便根据带有where子句的用户名从表中获取数据

这是我的php脚本

<?php

include_once 'connect.php';
$data = json_decode(file_get_contents("php://input"));


$statement = $conn->prepare('SELECT MessageId, Subject, MessageContent, CONCAT(Date, " , ", Time) AS Date, FromUser, ToUser, MessageType, File, FileName FROM messages where MessageType = "Broadcast" or ToUser=:UserName order by MessageId desc');
$statement->execute(array(
                          'UserName' => $data->UserCookie                          
                          ));

while($row = $statement->fetch()) {
    $data[] = $row;
}

print json_encode($data);
?>

这是我的角色剧本

var app = angular.module('Library', ["ngCookies"]);
app.controller('LibraryController', function($scope, $http, $interval, $cookies) {

        //get cookie user name
        $scope.UserCookie = $cookies.get('cookieUserName');

//set cookie user name
$scope.setCookie = function(val){
    $cookies.put('cookieUserName', val);
}



//display messages    
$scope.loadMessages = function() {
    $http.post('selectMessages.php',{'UserName': $scope.UserCookie})
    .success(function(data) {
        $scope.Messages = data;
    })       
}

//display messages at an interval of 1 seconds
$interval($scope.loadMessages, 1000);

});

这是我的主页

<!DOCTYPE html ng-app="Library" ng-controller="LibraryController">
<html>
<head>
  <title></title>
</head>
<body ng-init="loadMessages()">
      <div class="form-group">
      <label for="Subject">User</label>
        <input type="text" ng-model="UserCookie" name="UserCookie" class="form-control" id="UserCookie" required="required"></input>
      </div>


      <input type="search" class="form-control" ng-model="searchMessage" placeholder="Search"> <br />
      <table class="table table-hover">
        <thead>
          <tr>
            <th>Message (Sender)</th>
          </tr>
        </thead>
        <tbody>

          <tr ng-click="selectMessage(message.MessageId, message.MessageContent, message.Date, message.Time, message.File)" onclick="showCommentDialog()" style="font-size: 16px" ng-repeat="message in Messages | filter:searchMessage | limitTo:20">
           <td>{{ message.Subject }} (<span style="color:green"> {{ message.FromUser }})</span></td>
           <td>
            <button class="btn btn-success" ng-click="selectMessage(message.MessageId, message.MessageContent, message.Date, message.Time, message.File)" onclick="showCommentDialog()" > view </button>

          </td>
        </tr>
      </tbody>
    </table>

</body>
</html>

这是我得到的错误Error from browser console 请问我做错了什么。帮助!

2 个答案:

答案 0 :(得分:0)

请参阅this link寻求帮助。您的ng-repeat中有重复键。

答案 1 :(得分:0)

根据您更新的答案和您收到的错误,代码中存在一些隐藏的错误/问题。您正在获取输入,从json解码并将其保存在$data变量中(只要您没有将true作为第二个参数传递,json_decode将返回对象)。在while循环中,您尝试将新元素添加到$data,因为它是array,但事实并非如此。

以下是它的样子:

<?php

include_once 'connect.php';
$data = json_decode(file_get_contents("php://input"));

$outputData = [];

if($data && isset($data->UserCookie)) {
    $statement = $conn->prepare('SELECT MessageId, Subject, MessageContent, CONCAT(Date, " , ", Time) AS Date, FromUser, ToUser, MessageType, File, FileName FROM messages where MessageType = "Broadcast" or ToUser=:UserName order by MessageId desc');
    $statement->execute(array(
                              'UserName' => $data->UserCookie
                              ));

    while($row = $statement->fetch()) {
        $outputData[] = $row;
    }
}

print json_encode($outputData);
?>