Php Json编码获取重复数据
我创建用于在Smarty中列出数据的函数。它工作正常,但我的问题是我得到了两次数据。请帮助我 我的职能如下:Php Json编码获取重复数据,php,mysql,arrays,json,smarty,Php,Mysql,Arrays,Json,Smarty,我创建用于在Smarty中列出数据的函数。它工作正常,但我的问题是我得到了两次数据。请帮助我 我的职能如下: function appListing($requestvars) { try { $rh = $this->db->prepare('SELECT * FROM app'); //$rh->bindParam(':userOwnerId', $_SESSION['userId'], P
function appListing($requestvars)
{
try
{
$rh = $this->db->prepare('SELECT * FROM app');
//$rh->bindParam(':userOwnerId', $_SESSION['userId'], PDO::PARAM_STR);
$rh->execute();
$appData['App'] = $rh->fetchAll();
$this->smartyTemplate->assign('appData', $appData);
$this->smartyTemplate->assign('request', $requestvars);
$this->smartyTemplate->assign('homePath', APP_ROOT_DIR);
//$this->smartyTemplate->display('project/appList.html');
}
catch (PDOException $e)
{
$appData = "Error!: " . $e->getMessage();
}
echo json_encode($appData);
}
{
"App": [
{
"0": "app_57ba9fc847dd55_57218508",
"1": "旅行台南",
"2": "https://play.google.com/",
"3": "https://play.google.com/1471848392.png",
"4": "1471848392",
"5": "1471848392",
"appId": "app_57ba9fc847dd55_57218508",
"appName": "旅行台南",
"appURL": "https://play.google.com/",
"appImage": "https://play.google.com/1471848392.png",
"createTime": "1471848392",
"lastUpdateTime": "1471848392"
}
我得到的结果如下:
function appListing($requestvars)
{
try
{
$rh = $this->db->prepare('SELECT * FROM app');
//$rh->bindParam(':userOwnerId', $_SESSION['userId'], PDO::PARAM_STR);
$rh->execute();
$appData['App'] = $rh->fetchAll();
$this->smartyTemplate->assign('appData', $appData);
$this->smartyTemplate->assign('request', $requestvars);
$this->smartyTemplate->assign('homePath', APP_ROOT_DIR);
//$this->smartyTemplate->display('project/appList.html');
}
catch (PDOException $e)
{
$appData = "Error!: " . $e->getMessage();
}
echo json_encode($appData);
}
{
"App": [
{
"0": "app_57ba9fc847dd55_57218508",
"1": "旅行台南",
"2": "https://play.google.com/",
"3": "https://play.google.com/1471848392.png",
"4": "1471848392",
"5": "1471848392",
"appId": "app_57ba9fc847dd55_57218508",
"appName": "旅行台南",
"appURL": "https://play.google.com/",
"appImage": "https://play.google.com/1471848392.png",
"createTime": "1471848392",
"lastUpdateTime": "1471848392"
}
我希望输出为:
{
"App": [
{
"appId": "app_57ba9fc847dd55_57218508",
"appName": "旅行台南",
"appURL": "https://play.google.com/",
"appImage": "https://play.google.com/1471848392.png",
"createTime": "1471848392",
"lastUpdateTime": "1471848392"
}
使用
PDO::FETCH_ASSOC
。fetchAll()
的默认值是PDO::FETCH_BOTH
,它返回一个包含数字索引和命名索引的数组
$appData['App'] = $rh->fetchAll(PDO::FETCH_ASSOC);
使用
PDO::FETCH_ASSOC
。fetchAll()
的默认值是PDO::FETCH_BOTH
,它返回一个包含数字索引和命名索引的数组
$appData['App'] = $rh->fetchAll(PDO::FETCH_ASSOC);