繁体   English   中英

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

[英]fetch data from database using angularjs using where clause

我想使用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>

这是我从浏览器控制台收到的错误。请问我做错了什么。 救命!

请参阅此链接以获取帮助。 您的ng-repeat中有重复的键。

根据更新后的答案和收到的错误,代码中存在一些隐藏的错误/问题。 您将获得输入,从json解码并将其保存在$data变量中(只要您不将true作为第二个参数传递, json_decode返回object)。 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);
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM