PHP - 基于If else条件的多个选择查询

时间:2015-12-08 13:47:49

标签: php select mysqli

我需要基于If else条件的多选查询的帮助。我有多个表,我想像这样处理查询。

  1. 在table1中搜索,如果找到则返回结果。
  2. 如果未找到,则在表2中搜索。
  3. 如果在table1和table2中找不到单词,则在表3中检查,然后在表4中检查
  4. 我正在使用以下代码。

       $query = mysqli_query  ($db, "SELECT * FROM users WHERE email = '$email'") or die(mysqli_error($db));
    

2 个答案:

答案 0 :(得分:0)

str_replace($row['prefix'],'',$term );

答案 1 :(得分:0)

<?php
 mb_internal_encoding( 'UTF-8');



   $mysqli = new mysqli("localhost", "root", "", "us");
   if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
   }

   $mysqli->query("SET WORDS 'utf8'");
   $mysqli->query('SET CHARACTER SET utf8'); 


   $term = strip_tags(substr($_POST['searchit'],0, 100));
   $term = $mysqli->real_escape_string($term); 

   if($term=="") {
     echo "Enter Something to search";
     exit();
   }




   termcheck($term, $mysqli);             
   function termcheck($term, $mysqli){
     $qry="Select * from table1 where word ='$term' UNION Select * from table2 where word = '$term'";
     if($result = $mysqli->query($qry)){
        //echo "inside sql table1";
       $num_rows = $result->num_rows;
         if($num_rows > 0) {
            //echo "inside table1";
           while($row = $result->fetch_assoc())
             {
               echo "Stem : ".$row['term']."<br>";
             }
             exit();
         }
      }else {

           $qry1="Select * from table3";
           $result = $mysqli->query($qry1);
           $num_rows = $result->num_rows;
           if($num_rows > 0) {

             while($row = $result->fetch_assoc()){
                echo "inside while";
               if($table4=mb_strrichr($term,$row['prefix'])){
                echo "inside if";
                  $sterm=str_replace($row['prefix'],'',$term);
                  $postfix=$row['prefix'];
                echo "inside : ".$sterm;
                }
             }
           }else{echo "Error : Table3 doesn't exist";}
         }

         if(!empty($sterm)){
           $qry3="Select * from table4";
           $result3 = $mysqli->query($qry3);
           $num_rows = $result3->num_rows;
           if($num_rows > 0) {
             while($row = $result3->fetch_assoc()){
               if(mb_strrichr($sterm,$row['postfix'])){
                  $ssterm=str_replace($row['postfix'],'',$sterm);
                  $prefix=$row['postfix'];
                  echo "prefix : ".$prefix;
                  echo "<br>";
                  echo "postfix : ".$postfix;
                  echo "<br>";
                  echo "sterm : ".$ssterm;
                } 
             }exit();
           }else{echo "Error : Table3 doesn't exist";}
         }else{
           $qry3="Select * from table4";
           $result3 = $mysqli->query($qry3);
           $num_rows = $result3->num_rows;
           if($num_rows > 0) {
             while($row = $result3->fetch_assoc()){
               if(mb_strrichr($term,$row['postfix'])){
                  $prefix=$row['postfix'];
                  $sterm=str_replace( $row['postfix'],'',$term);
                  echo "prefix : ".$prefix;
                  echo "<br>";
                  echo "sterm : ".$sterm;
               }
             }exit();
           } else{echo "Error : Table3 doesn't exist";}          
         }
       }


  ?>