我在使用PHP从SQL服务器获取的数组内容作为离子框架上的JSON时遇到了问题。
数组(或者至少是我得到的输出)包含多个重复的数据,这些数据甚至不存在于我的数据库中。即表中的前3行是{1,2,3},我会得到类似{1,3,3,2,2,1,2,2,3}左右的东西。
这是我的.php文件,
<?php
header('Access-Control-Allow-Origin: *');
$data = array();
$id = filter_var($_REQUEST['id'], FILTER_SANITIZE_NUMBER_INT);
// Attempt to query database table and retrieve data
try {
$sql = "SELECT table1.id, table1.name
FROM table1
INNER JOIN table2
ON table1.id= :id";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_OBJ))
{
// Assign each row of data to associative array
$data[] = $row;
}
//while ()
// Return data as JSON
echo json_encode($data);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
这是我的代码段.ts文件
load()
{
let body : string = "id=" + this.id,
type : string = "application/x-www-form-urlencoded; charset=UTF-8",
headers : any = new Headers({ 'Content-Type': type}),
options : any = new RequestOptions({ headers: headers }),
url : any = "https://happymeal.000webhostapp.com/retrieve-foo.php";
this.http.post(url, body, options)
//this.http.get('https://happymeal.000webhostapp.com/retrieve-food.php')
.map(res => res.json())
.subscribe(data =>
{
this.items = data;
});
}
这是负责显示数据的.html文件的片段
<ion-item *ngFor="let item of items">
<div class="row">
<div class="col">{{item.id}}</div>
<div class="col">{{item.name}}</div>
<div class="col"><button class="button" (click)="actionFunc({ record: item })">Edit Name</button></div>
</div>
</ion-item>