使用php比较mysql结果,然后回显差异?

时间:2015-10-21 15:22:20

标签: php mysql

我是PHP的新手,我正在试图弄清楚如何比较两个mysql查询结果并回应差异。基本上我有一个比较状态标志特征的数据库。用户将选择2个状态,我想要的是让php回显3组结果(仅状态1,共享特征和状态2)。我下面的粗略代码只回显了状态1和2的特征。有没有一种方法可以比较查询结果,这样我就可以得到状态之间的差异,也可以分享特征。

<?php  $query1 = $_GET['query1'];   
       $query2 = $_GET['query2'];  
       $min_length = 2;?>

<!-- state 1 characteristics only -->

<div class="fluid s1charc">

<?php echo "<strong><p>Search results for $query1</strong></p>";?>

<?php if(strlen($query1) >= $min_length){$query1 = htmlspecialchars($query1);
      $query1 = mysql_real_escape_string($query1);
      $raw_results1 = mysql_query("SELECT * FROM charc_s WHERE `charc_f` ='$query1' ORDER BY FLAG_S") or die(mysql_error());
      if(mysql_num_rows($raw_results1) > 0) { 
      while($results1 = mysql_fetch_array($raw_results1)){

      echo "<table id='iseqchart' class='sortable'>
               <tr valign='middle'>
                   <td ><a href='flag.php?FLAG_ID=".$results1['FLAG_ID']."'><img src='images/".$results1['IMAGE']."'></a></td>
                   <td>&nbsp;</td>
                   <td><strong><font size='3'><a href='flag.php?FLAG_ID=".$results1['FLAG_ID']."' style='color:#100783'>".$results1['FLAG_S']."&nbsp;(".$results1['STATE'].")</strong></a>                 
                   </td>
               </tr>
              </table>";
            }
        }
        else{ // if there is no matching rows do following
            echo "No results were found";
        }
    }
    else{ // if query length is less than minimum
        echo " ".$min_length;
    }
?> 

</div>

<!-- shared characteristics -->
<div class="fluid sharecharc">Shared</div>

<!-- state 2 characteristics only -->
<div class="fluid s2charc">
<?php echo "<strong><p>Search results for $query2</strong></p>"; ?>

<?php  if(strlen($query2) >= $min_length){$query2 = htmlspecialchars($query2); 
       $query2 = mysql_real_escape_string($query2);  
       $raw_results = mysql_query("SELECT * FROM charc_s WHERE `charc_f` ='$query2' ORDER BY FLAG_S") or die(mysql_error());
       if(mysql_num_rows($raw_results) > 0) {  
       while($results = mysql_fetch_array($raw_results)){
       echo "<table id='iseqchart' class='sortable'>
               <tr valign='middle'>
                   <td ><a href='flag.php?FLAG_ID=".$results['FLAG_ID']."'><img src='images/".$results['IMAGE']."'></a></td>
                   <td>&nbsp;</td>
                   <td><strong><font size='3'><a href='flag.php?FLAG_ID=".$results['FLAG_ID']."' style='color:#100783'>".$results['FLAG_S']."&nbsp;(".$results['STATE'].")</strong></a>                
                   </td>
               </tr>
              </table>";
            }
        }
        else{ // if there is no matching rows do following
            echo "No results were found";
        }  
    }
    else{ // if query length is less than minimum
        echo " ".$min_length;
    }
?>

</div>

1 个答案:

答案 0 :(得分:0)

执行此操作的最佳方法是在服务器端。使数据库做3种不同的选择。原因是服务器将比php快得多地运行查询。它最终会带来更好的用户体验。

仅表A

 SELECT * FROM TableA A LEFT JOIN TableB B ON A.key = B.key

仅表B

 SELECT * FROM TableA A RIGHT JOIN TableB B ON A.key = B.key

并在两个表中

 SELECT * FROM TableA A INNER JOIN TableB B ON A.key = B.key

从那里回显你的结果。