我是使用mysql的php web服务的新手。我遵循这个教程。我创建了一个表-> loan_applications
使用phpmyadmin。当前我在表中有3条记录与id 1相关。我想检索所有3个记录,并想在json编码它。
我尝试了多种方式,并尝试谷歌搜索,但无法得到正确的json数组的响应。这是我的 get_applications_list.php
```
这是我的 `DB_Functions.php` ```
<?php
class DB_Functions {
private $conn;
// constructor
function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$db = new Db_Connect();
$this->conn = $db->connect();
}
// destructor
function __destruct() {
}
public function getApplicationsList($id){
$stmt = $this->conn->prepare("SELECT * FROM loan_applications WHERE id = ?");
$stmt->bind_param("s", $id);
$stmt->execute();
$applications = $stmt->get_result()->fetch_assoc();
$stmt->close();
if($applications){
return $applications;
}else {
return false;
}
}
}
?>
以下是我得到的回应:
{"status":0,"id":1,"application_id":1,"requested_amount":5000,"interest_per_day":"0.50","gst":18,"tenure":28,"processing_fees":"5.00","amount_user_get":4705,"amount_user_pay":5700,"application_latitude":"9.999999999","application_longitude":"9.999999999","application_status":1,"created_at":"2018-10-10 21:45:17","updated_at":"0000-00-00 00:00:00","message":"Applications details fetched successfully"}
我只得到一个记录,但我需要所有3个记录与id 1。我试了很多次,但没能成功。
1条答案
按热度按时间tv6aics11#
所以这里有很多问题
1-虽然不确定但是
Currenlty I have 3 records in table related to id 1
似乎不正确的说法。如果id
是主键,一条记录不能有3条记录id
2 -$stmt->get_result()->fetch_assoc();
将始终返回一行,若要获取多行或行集合,您需要按如下所示执行3-从下面的代码中可以很清楚地看出,实际上您只返回了一行
你应该这样做