简体   繁体   English

如果可能的话,使用PHP从JSON中的两个MySQL表获取数据

[英]Get data from two MySQL tables in JSON using PHP if possible use joins

<?php  
$db=new PDO('mysql:dbname=fvilla;host=localhost;','root','');  
$row=$db->prepare('select * from slider');  
$row->execute();//execute the query  
$json_data=array();//create the array  
foreach($row as $rec)//foreach loop  
{  
    $json_array['id']=$rec['id'];  
    $json_array['image_url']=$rec['image_url'];  
    $json_array['link_image']=$rec['link_image'];  
    array_push($json_data,$json_array);  

}  
echo json_encode($json_data);  
$row1=$db->prepare('select * from collections');  
$row1->execute();//execute the query  
$json_data1=array();//create the array  
foreach($row1 as $rec1)//foreach loop  
{  
    $json_array1['cid']=$rec1['id'];  
    $json_array1['cimage_url']=$rec1['image_url'];  
    $json_array1['cheading']=$rec1['heading']; 
    $json_array1['csubheading']=$rec1['subheading'];  
    array_push($json_data1,$json_array1);  
}  
echo json_encode($json_data1);  
?>

and the output is coming like that : 输出是这样的:

[{"id":"1","image_url":"http:\/\/fvilla.in\/app\/slider\/first.jpg","link_image":""},{"id":"2","image_url":"http:\/\/fvilla.in\/app\/slider\/second.jpg","link_image":""},{"id":"3","image_url":"http:\/\/fvilla.in\/app\/slider\/third.jpg","link_image":""}]

[{"cid":"1","cimage_url":"http:\/\/fvilla.in\/app\/collections\/1.jpg","cheading":"Body Art","csubheading":"Tattoo, Piercing & More"},{"cid":"2","cimage_url":"http:\/\/fvilla.in\/app\/collections\/2.jpg","cheading":"Nail Care","csubheading":"Manicure, Nail Art & More"},{"cid":"3","cimage_url":"http:\/\/fvilla.in\/app\/collections\/3.jpg","cheading":"Hair Care","csubheading":"Straightening, Rebonding & More"},{"cid":"4","cimage_url":"http:\/\/fvilla.in\/app\/collections\/4.jpg","cheading":"Bridal Box","csubheading":"Bridal Makeup, Hair & More"},{"cid":"5","cimage_url":"http:\/\/fvilla.in\/app\/collections\/5.jpg","cheading":"Face Care","csubheading":"Facial, Makeup & More"},{"cid":"6","cimage_url":"http:\/\/fvilla.in\/app\/collections\/6.jpg","cheading":"Mehandi Show","csubheading":"Mehandi For Hand & Feet"}]

but I want the output in this format: 但我想要这种格式的输出:

{
   "slider1" : [{"id":"3","image_url":"http:\/\/fvilla.in\/app\/slider\/third.jpg","link_image":""},{"id":"6","image_url":"http:\/\/fvilla.in\/app\/slider\/third.jpg","link_image":""}],
   "slider2" : [{ },{ }]
}

Not entirely sure that this will suffice, but something like this might be what you are after: 不能完全确定这是否足够,但是您可能会想要这样:

$db=new PDO('mysql:dbname=fvilla;host=localhost;','root','');  
$output=array();

$row=$db->prepare('select * from slider');  
$row->execute();
$sliders=array();
foreach( $row as $key => $rec ){  
    $sliders[ 'slider'.$key ]=array('id' => $rec['id'],'image_url' => $rec['image_url'],'link_image' => $rec['link_image']);
} 
$output['sliders']=$sliders;
#echo json_encode( $sliders );  



$row=$db->prepare('select * from collections');  
$row->execute();
$collections=array();
foreach( $row as $key => $rec ){  
    $collections [ 'collections'.$key ]=array('cid' => $rec['id'],'cimage_url' => $rec['image_url'],'cheading' => $rec['heading'],'csubheading' => $rec['subheading'] );
}
$output['collections']=$collections;
#echo json_encode( $collections );

echo json_encode( $output );

Well, you can create your own array to hold your results from one table. 好了,您可以创建自己的数组来保存来自一个表的结果。

<?php  
$db=new PDO('mysql:dbname=fvilla;host=localhost;','root','');  
$row=$db->prepare('select * from slider');  
$row->execute();//execute the query  
$json_data=array();//create the array
$result = array();
foreach($row as $rec)//foreach loop  
{  
    $json_array['id']=$rec['id'];  
    $json_array['image_url']=$rec['image_url'];  
    $json_array['link_image']=$rec['link_image'];  
    array_push($json_data,$json_array);  

}  
$result["table1"] = $json_data;

$row1=$db->prepare('select * from collections');  
$row1->execute();//execute the query  
$json_data1=array();//create the array  
foreach($row1 as $rec1)//foreach loop  
{  
    $json_array1['cid']=$rec1['id'];  
    $json_array1['cimage_url']=$rec1['image_url'];  
    $json_array1['cheading']=$rec1['heading']; 
    $json_array1['csubheading']=$rec1['subheading'];  
    array_push($json_data1,$json_array1);  
}
$result['table2'] = $json_data1;
echo json_encode($result);  
?>

You can use UNION ALL to get all data from both tables in a single query ie 您可以使用UNION ALL在单个查询中从两个表中获取所有数据,即

$db=new PDO('mysql:dbname=fvilla;host=localhost;','root','');  
$row=$db->prepare('select id as slider_id, image_url as slider_image, heading, subheading from slider UNION ALL select id as header_id, image_url as header_image from collections');  
$row->execute();//execute the query  
$result = $row->fetchAll(PDO::FETCH_ASSOC);
$json_array = array();
foreach($result as $rec)//foreach loop  
{  
    $json_array['slider1']['id']=$rec['slider_id'];  
    $json_array['slider1']['image_url']=$rec['slider_image'];  
    $json_array['slider1']['link_image']=$rec['link_image'];   

    $json_array['slider2']['cid']=$rec['header_id'];  
    $json_array['slider2']['cimage_url']=$rec['header_image'];  
    $json_array['slider2']['cheading']=$rec['heading']; 
    $json_array['slider2']['csubheading']=$rec['subheading'];  

}
echo json_encode($json_array);

NOTE:- 注意:-

Use UNION ALL to collect all data from two queries, since you have id, image_url common fields in both table so use alias as with different key as mentioned in query 使用UNION ALL可以从两个查询中收集所有数据,因为两个表中都有id, image_url公用字段,因此请使用别名as查询中提到的不同键

Hope this works, Happy coding... 希望这行得通,祝您编程愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM