Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-基于If-else条件的多个Select查询_Php_Select_Mysqli - Fatal编程技术网

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

PHP-基于If-else条件的多个Select查询,php,select,mysqli,Php,Select,Mysqli,我需要基于If-else条件的多个Select查询的帮助。我有多个表,我想这样处理查询 在表1中搜索,如果找到,则返回结果 如果未找到,则在表2中搜索 如果在表1和表2中都找不到单词,则检查表3,然后检查表4 我使用下面的代码 <?php // 1. Create a database connection $mysqli = new mysqli("localhost", "root", "", "us"); /* check connectio

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

  • 在表1中搜索,如果找到,则返回结果
  • 如果未找到,则在表2中搜索
  • 如果在表1和表2中都找不到单词,则检查表3,然后检查表4
  • 我使用下面的代码

            <?php
        // 1. Create a database connection
        $mysqli = new mysqli("localhost", "root", "", "us");
    
        /* check connection */
        if ($mysqli->connect_errno) {
            printf("Connect failed: %s\n", $mysqli->connect_error);
            exit();
        }
    
        $term = strip_tags(substr($_POST['searchit'],0, 100));
        $term = $mysqli->real_escape_string($term); 
    
        if($term=="") {
        echo "Enter Something to search";
            exit();
        }
    
    
        if(
            $result = $mysqli->query("Select * from table1 where word like '{$term}%'
                            UNION
                            Select * from table2 where word like '{$term}%'")){
    
           $num_rows = $result->num_rows;
            if(mysqli_num_rows($result) >= 0) {
                while($row = $result->fetch_assoc())
                 {
            echo "Stem : {$row['word']} <br>";
        }
        }
    
            else {
                 $result = $mysqli->query("Select * from table3 where words like '{$term}%'");
    
                        $num_rows = $result->num_rows;
            if(mysqli_num_rows($result) >= 0) {
                while($row = $result->fetch_assoc())
                 {
            echo "words: {$row['words']} <br>";
        }
        }
    }
    }
    
        else{
         echo "No matches found!";
        }
    
        ?>
    
    试试看

    试一试


    是否要从
    $term
    中删除
    $row['prefix']
    ?能否提供表3、表2、表1、输入和预期结果的示例,以便我们能够准确地告诉您要查找的内容?是否要从
    $term
    中删除
    $row['prefix']
    ?能否提供表3、表2、表1的示例,输入,以及预期的结果,以便我们能够准确地知道您在寻找什么?谢谢您的回复。因为我的table3查询已经没有显示任何结果,所以我需要在哪个特定位置应用此功能。谢谢您的回复。因为我的table3查询已经没有显示任何结果,所以我需要在哪个特定位置应用此选项。我在此处看不到以前的注释。你收到我之前的评论了吗?对不起,有点忙,你想什么时候完成?我想尽快完成。如果你有多余的时间,请帮忙。非常感谢您的帮助。您的语言没有定义函数,这就是问题所在。那么解决方案是什么?我在这里看不到我以前的评论。你收到我之前的评论了吗?对不起,有点忙,你想什么时候完成?我想尽快完成。如果你有多余的时间,请帮忙。非常感谢您的帮助。您的语言没有定义函数,这就是问题所在。那么解决方案是什么?
    str_replace($row['prefix'],'',$term );
    
    <?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";}          
             }
           }
    
    
      ?>