weixin_33713350 2016-07-19 06:23 采纳率: 0%
浏览 556

json对象中只有一个数组

I have 5 tables for different files that user can upload. I would like to get all the files with corresponding data in one array so 5 files data in one array.

All the tables have same structure:

user_id, file_type, file_size, file_name, file_new_name, file_path, date_created 

In PHP I am using this query select * from degree_files, profile_pictures, cpr_files, backgroundcheck_files, video_files where degree_files.user_id = :user_id which gives me all 5 file data if I run it in workbench, but json object is only one array.

How can I show all the data in one array or what would be the right solution, because right now I am querying each table separately?

I tried:

$backgroundCheck = $user_home->runQuery("select * from degree_files, profile_pictures, cpr_files, backgroundcheck_files, video_files where degree_files.user_id = :user_id");
$backgroundCheck->execute(array(":user_id"=>$user_id));
$nanny_backgroundCheck_file = $backgroundCheck->fetchAll(PDO::FETCH_ASSOC);


$file_name = array();
foreach ($nanny_backgroundCheck_file as $c){
    $file_name[] = $c['file_name'];
}

$arr[] = array('file_name'=>$file_name);
echo json_encode($arr, JSON_UNESCAPED_UNICODE);

And AJAX call:

$.getJSON("PHP/nannyInfo.php", function(data) {
        //SELECT2 DATA
        $.each(data.nanny_backgroundcheck_file, function(index, data) {
        console.log(data);

        });

});

What I see in console.

enter image description here

  • 写回答

1条回答 默认 最新

  • 乱世@小熊 2016-07-19 07:54
    关注

    The query first. You're trying to get a single column from multiple tables, right? If so, I'd suggest this syntax:

    $sql = "
        select file_name from degree_files where user_id = :user_id
        UNION select file_name from profile_pictures where user_id = :user_id
        UNION select file_name from cpr_files where user_id = :user_id
        UNION select file_name from backgroundcheck_files where user_id = :user_id
        UNION select file_name from video_files where user_id = :user_id";
    

    Then, the actual SQL execution is mostly OK but I'd simplify it to:

    $backgroundCheck = $db->prepare($sql);
    $backgroundCheck->execute(array(":user_id"=>1)); // 
    $fileNames = $backgroundCheck->fetchAll(PDO::FETCH_COLUMN, 'file_name');
    
    echo json_encode($fileNames, JSON_UNESCAPED_UNICODE);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用R语言GD包一直不出结果
  • ¥15 计算机微处理器与接口技术相关问题,求解答图片的这个问题,有多少个端口,端口地址和解答问题的方法和思路,不要AI作答
  • ¥15 如何根据一个截图编写对应的HTML代码
  • ¥15 谁能远程帮我装好软件,破解覆盖主程序,启动后左侧选择字典,输入单词,报酬15元,加我微信15218392686
  • ¥15 stm32标准库的PID角度环
  • ¥15 ADS已经下载好了,但是DAS下载不了,一直显示这两种情况,有什么办法吗,非常急!
  • ¥100 Excel 点击发送自动跳转outlook邮件
  • ¥15 gis中用栅格计算器或加权总和后图层不显示,值也明显不对
  • ¥15 python使用python-pptx如何给幻灯片添加只读密码。
  • ¥15 深度神经网络传递自变量损失