【问题标题】:Compare values of two tables and for results export JSON比较两个表的值并为结果导出 JSON
【发布时间】:2012-10-30 04:57:05
【问题描述】:

我有两张不同的桌子。对于那些我选择一些结果。 第一个基于最小/最大值,第二个基于 Lat/Lng。这很容易(不要太在意这些值)并且由以下人员创建:

$sql1="SELECT * FROM events WHERE value BETWEEN '".$min2d."' AND '".$max2d."'";
$sql2="SELECT * FROM locations WHERE (lng BETWEEN ".$wl." AND ".$el.") AND (lat BETWEEN '".$sl."'AND'".$nl."')";

现在我们已经缩小了结果的大小,如果第二个表存在,我们希望匹配第一个表的'id' 行。成功后我们创造成果。

所以让我们先得到一些数字:

$result1 = mysql_query($sql1);
$result2 = mysql_query($sql2);

$numRows1 = mysql_num_rows($result1);
$numRows2 = mysql_num_rows($result2);

$loopCount1 = 1;
$loopCount2 = 1;

为了更有效地解析 JSON(来自用户),我们希望通过将事件创建到 JSON 数组中来对事件进行排序,并将位置作为“持有者”。这意味着,每个位置可能有几个事件。

某些位置可能没有事件,但也有一些事件可能与某个位置不对应。我们唯一的比较方法是通过相同的'id'

通过我的以下尝试,这当然是为 all (这是错误的)位置结果创建的错误,即使对于那些没有事件的位置结果也是如此。这就是我需要你宝贵帮助的地方。

$json = '{"markers":[';
while ($row2 = mysql_fetch_array($result2)){
    $json .= '{"coordinates": { "lat":'. $row2['lat'] .', "lng" : ' . $row2['lng'] .'},'
            .'{"events" : [';
    while ($row1 = mysql_fetch_array($result1)){
        if ($row1['id']=$row2['id'])
            $json .= '{ '   
                    .'"title": "'.$row1['title'].'",'
                    .'"info": "'.$row1['info'].'",'
                    .'"}';
        // add comma for Json if not final row
        if ($loopCount1 < $numRows1) {
            $json .= ', ';
            $loopCount1++;}
        }
    $json .= ']}}';
    // add comma for Json if not final row
    if ($loopCount2 < $numRows2) {
            $json .= ', ';
            $loopCount2++;}
    }
$json .= ']}';

最后是回声:

echo $json;

【问题讨论】:

  • 您是否有理由不使用 JOIN 而不是在两个查询之间进行自己的匹配?为什么要手动创建 JSON 而不是使用json_encode()
  • 你的内部 while 循环只会在外部循环的第一次迭代中起作用,因为它会获取 $sql1 的所有结果,并且下次没有任何东西可以获取。您需要在内部循环之前执行mysql_data_seek($result1, 0); 以将其重置为开头。

标签: php mysql compare json nested-loops


【解决方案1】:

要比较两个表的值并将结果打印为 json,请使用以下代码,

<?php
      /* connect to the db */
      $link = mysql_connect('localhost','username','password') or die('Cannot connect to the DB');
      mysql_select_db('db_name',$link) or die('Cannot select the DB');

      /* grab the posts from the db */
      $query1 = "SELECT * FROM test1";
      $result1 = mysql_query($query1,$link) or die('Error query:  '.$query1);

      $query2 = "SELECT * FROM test2";
      $result2 = mysql_query($query2,$link) or die('Error query:  '.$query2);

      /* create one master array of the records */
      $posts = array();
      if(mysql_num_rows($result1) && mysql_num_rows($result2)) {
        while($post1 = mysql_fetch_assoc($result1)) {
            $post2 = mysql_fetch_assoc($result2);
            if($post1['name'] == $post2['name'])
              $posts[] = array('test1'=>$post1,'test2'=>$post2);
        }
      }

     header('Content-type: application/json');
     echo json_encode(array('posts'=>$posts));

      /* disconnect from the db */
      @mysql_close($link);
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 2013-08-27
    相关资源
    最近更新 更多