使用多个数据库的JSON Web服务

时间:2014-06-12 12:31:55

标签: php jquery json database web-services

我有一个使用JSON格式的PHP构建的Web服务。 Web服务需要从两个不同的数据库中获取数据,

第一个DB with table posts1

id | firstnane |姓氏|标题|图像

第二个DB with table posts2

id |经验|指定|公司|位置

这是我的代码

   <?php
$connection1=mysqli_connect("localhost","root","",'json_data_db1');
$connection2=mysqli_connect("localhost","root","",'json_data_db2');
// queries for 1st connection
$query  = "select firstname,lastname,title,url from posts where id='6'";
$sql=mysqli_query($connection1,$query) or die(mysql_error());

echo '{"posts": [';

while($row=mysqli_fetch_array($sql))
{
$firstname=$row['firstname'];
$lastname=$row['lastname'];
$title=$row['title'];
$url=$row['url'];
echo '{"firstname":"'.$firstname.'","lastname":"'.$lastname.'","title":"'.$title.'","image":"'.$url.'"},';
}
//echo ']}';
//echo '{"profession": [';

// queries for 2nd connection 
$query  = "select * from posts2 where id='6' limit 20";
$sql=mysqli_query($connection2,$query);

    while($row=mysqli_fetch_array($sql))
    {
        $exp=$row['exp'];
        $des=$row['des'];
        $company=$row['company'];
        $location=$ow['location'];
        echo '{"experience":"'.$exp.'","designation":"'.$des.'","company":"'.$company.'","location":"'.$location.'"},';     

    }

echo ']}';

mysqli_close($connection1);
mysqli_close($connection2);
?>

结果的json数据是

  

{     “帖子”:[       {         “名字”:“devika”,         “姓氏”:“v”,         “title”:“海德拉巴”,         “image”:“Chrysanthemum.jpg”       },       {         “经验”:“软件工程师”,         “指定”:“软件工程师”,         “公司”:“Topnottch”,         “地点”: ””       },       ]       }

我的解析JSON数据的代码如下。

<script type="text/javascript">
$(function()
{
$(document).ready(function()
{
$.getJSON("json_data.php",function(data)
{

$.each(data.posts, function(i,data)
{

var div_data =
"<div ><table width='500' border='1'><tr><tr><td>"+data.firstname+"</td><td>"+data.lastname+"</td><td>"+data.title+"</td><td><img src='images/"+data.image+"' width='40' height='40'></td><td>"+data.experience+"</td><td>"+data.designation+"</td><td>"+data.company+"</td><td>"+data.location+"</td></tr></table></div>";
$(div_data).appendTo("#data_area");

});
}
);
return false;
});
});
</script>
<div id="data_area"></div>

它解析了名字,姓氏,头衔和图像。但它没有解析经验,名称,公司和位置。任何人都可以帮助我。

2 个答案:

答案 0 :(得分:0)

这应该有所帮助:

$connection1 = mysqli_connect("localhost", "root", "", 'json_data_db1');
$connection2 = mysqli_connect("localhost", "root", "", 'json_data_db2');
// queries for 1st connection
$query = "select id,firstname,lastname,title,url from posts where id='6'";
$sql = mysqli_query($connection1, $query) or die(mysql_error());

$data = array();
while ($row = mysqli_fetch_array($sql)) {
    // just save that with the id as key
    $data[$row['id']] = $row;
}

// queries for 2nd connection 
$query = "select * from posts2 where id='6' limit 20";
$sql   = mysqli_query($connection2, $query);

while ($row = mysqli_fetch_array($sql)) {
    // use the same entry from above
    if (isset($data[$row['id']])) {
        $data[$row['id']]['exp'] = $row['exp'];
        $data[$row['id']]['des'] = $row['des'];
        $data[$row['id']]['company'] = $row['company'];
        $data[$row['id']]['location'] = $row['location'];
    }
}

// dont encode yourself, just use this
echo json_encode(array_values($data));

mysqli_close($connection1);
mysqli_close($connection2);

答案 1 :(得分:0)

试试这个解决方案

<?php

        $connection1=mysqli_connect("localhost","root","",'json_data_db1');
        $connection2=mysqli_connect("localhost","root","",'json_data_db2');
        // queries for 1st connection
        $query  = "select firstname,lastname,title,url from posts where id='6'";
        $sql=mysqli_query($connection1,$query) or die(mysql_error());  


        while($row=mysqli_fetch_array($sql))
        {
        $firstname=$row['firstname'];
        $lastname=$row['lastname'];
        $title=$row['title'];
        $url=$row['url'];
        $result[0] = array("firstname" => $firstname,
              "lastname" => $lastname,
              "title" => $title,
              "image" =>$url );
        }

        // queries for 2nd connection 
        $query  = "select * from posts2 where id='6' limit 20";
        $sql=mysqli_query($connection2,$query);

            while($row=mysqli_fetch_array($sql))
            {
                $exp=$row['exp'];
                $des=$row['des'];
                $company=$row['company'];
                $location=$ow['location'];
                $result[1] = array("experience" => $exp,
                "designation" => $des,
                 "company" => $company,
                 "location" =>$location );    

            }

       $final_result['posts'] = array_merge($result[0],$result[1]);
        echo json_encode($final_result);

        mysqli_close($connection1);
        mysqli_close($connection2);
        ?>

所以你会得到像

这样的结果
{ "posts": { "firstname": "devika", "lastname": "v", "title": "Hyderabad", "image": "Chrysanthemum.jpg","experience": "software engineer", "designation": "Software Engineer", "company": "Topnottch", "location": "" }};

然后你将在jquery上使用这个

得到两个对象
$.each(data,function(key,value){
  console.log(value.firstname);
  console.log(value.company);

});