dougou5844 2014-11-02 14:03
浏览 51
已采纳

只获得一条关于mysql数据调用的记录(angular-slimphp)

I am trying to use the slim php framework as a restful backend to an angular frontend. I'm having problems fetching all records from my sql database. I am using the following code in my controller:

    function refreshlistings() {
    Data.get('getAllListings').then(function(results) {
        $scope.lists = results;
        var tempdata;
        for (var i = 0; i < $scope.lists.length; i++) {
            $scope.lists[i].data = decrypt($scope.lists[i].data);
        }

        if (!$scope.lists) {
            $scope.lists = [];
        }

    });
}

in my factory:

obj.get = function(q) {
        return $http.get(serviceBase + q).then(function(results) {
            return results.data;
        });
    };

And my slim API looks like this:

$app->get('/getAllListings', function(){
$db = new DbHandler();
$listings = array();
$listings = $db->getAllRecords("select * from LISTINGTABLE");
echoResponse(200, $listings);
});

in my DbHandler class:

public function getAllRecords($query) {
    $r = $this->conn->query($query) or die($this->conn->error.__LINE__);
    return $result = $r->fetch_assoc();    
}

I am only getting back one record from my SQL.

I tried:

public function getAllRecords($query) {
    $r = $this->conn->query($query) or die($this->conn->error.__LINE__);
    while ( $row = $r->fetch_array(MYSQLI_ASSOC) ) {
    $data[] = $row;
    }
    return $data[];    
}

In my DbHandler class, but that gives me an error. Can anyone help me out? thx!

  • 写回答

1条回答 默认 最新

  • douqianrou9079 2014-11-02 14:13
    关注

    You only request one row. If you use PDO you can do this:

    $q = $db->query("SELECT ... "); $results = $q->fetchAll();

    If you use mysqli, then you must traverse the result set and assign rows to an array.

    Also, I see in your code that you return $data[]. You should return $data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺