【问题标题】:JSON webservice using multiple databases使用多个数据库的 JSON 网络服务
【发布时间】:2014-06-12 12:31:55
【问题描述】:

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

第一个带有表 posts1 的数据库

标识 |第一烷 |姓氏 |标题 |图片

第二个数据库与表posts2

标识 |经验 |名称 |公司|位置

这是我的代码

   <?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数据是

{ “帖子”:[ { “名字”:“德维卡”, “姓氏”:“v”, "title": "海得拉巴", “图像”:“菊花.jpg” }, { “经验”:“软件工程师”, “职称”:“软件工程师”, “公司”:“顶尖”, “地点”: ”” }, ] }

我解析 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>

它解析了名字,姓氏,标题和图像。但它不解析经验,名称,公司和位置。谁能帮我。

【问题讨论】:

  • 我想这给了你两张桌子。一个填充了第一个数据库的信息,一个填充了第二个数据库的信息。这就是我阅读您的代码的方式。
  • 是的。但它不显示来自第二个数据库表的数据
  • 你的 json 输出不是一个有效的 json 在这里试试 jsonlint.com

标签: php jquery json database web-services


【解决方案1】:

试试这个解决方案

<?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 上使用 this 得到两个对象

$.each(data,function(key,value){
  console.log(value.firstname);
  console.log(value.company);

});

【讨论】:

  • 这里我用javascript给出了显示,但它的结果未定义 $.each(data.posts,function(key,value){ var div_data ="
    "+data.firstname+" "+data.experience+"
    " $(div_data).appendTo("#data_area"); });
  • @Devika514 不是 data.posts。试试数据。
  • @Devika514 代码必须是 $.each(data,function(key,value){ var div_data ="
    ‌​table>" $(div_data).appendTo("#data_area"); });
  • 是的。我试过了。但它也显示未定义
  • 你的 json 响应是什么。请在此处粘贴
  • "+value.firstname+" "+value.experience+"
【解决方案2】:

这应该会有所帮助:

$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);

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签