Fetching/Putting mysqli_num_row 中的 2 个变量和 PHP 中的 mysqli_fetch_array

Fetching/Putting 2 variables in a mysqli_num_row and mysqli_fetch_array in PHP

基本上我想按类别和标题进行搜索。我的问题是两者位于不同的 table 中。我正在考虑在 mysqli_num_rowsmysqli_fetch_array 中放置 2 个变量,但我认为这不是正确的想法。 $search 变量已经在工作,但我不知道我会为 $searchcat 做些什么,这是一个不同的 table.

<table border="1">
    <tr>
        <th>ID</th>
        <th>Survey Title</th>
        <th>Category</th>

    </tr>

           <?php
    if (isset($_POST['submit']))
    {
        include 'testdb.php'; //connection is written in other page (db.php)
        $var =$_POST['search'] ;
        $searchtype = $_POST['searchtype'];
        $my_query="SELECT s.survey_id, s.title,c.categoryname 
           FROM survey_header as sh 
           JOIN survey   AS s ON sh.survey_id=s.survey_id
           JOIN category AS c ON sh.category_id=c.category_id
           WHERE $searchtype LIKE '%".$var."%' 
           ORDER BY title ASC";

        $get_data= mysqli_query($con, $my_query) or die ("Couldn't execute query: ".mysqli_error());


        if (mysqli_num_rows($get_data) > 0 )
        {
            echo "<h3>Search results:</h3>";
            while($show = mysqli_fetch_array($get_data))
            {
                $id = $show['survey_id'];
                $title = $show['title'];
                $category = $show['categoryname']; //


                echo "<tr align='center'>";
                echo "<td><font color='black'>" .$id. "</font></td>";
                echo "<td><font color='black'>" .$title. "</font></td>";
                echo "<td><font color='black'>" .$category. "</font></td>";


            }
        }
        else{
            echo "No Records found!";
        }

    }


    ?>
</table>
</body>

这是table类别(类别名称是我需要的)

     +-------------+---------------+-------------+
    | category_id | categoryname  | datecreated |
    | 1           | Philosophical |             |
    | 4           | Political     |             |
    | 6           | Social        |             |

这是table调查(我只需要标题)

| 1 | survey_id | title | description  | duration | gender | age_group_from | age_group_to |
| 2 | 44        | game1 | description1 | 5        | male   | 0              | 18           |
| 3 | 45        | game2 | description2 | 25       | female | 18             | 25           |
| 4 | 46        | game3 | description3 | 89       | female | 26             | 35           |  

这是tablesurvey_header(survey_id和category_id是我需要的)

    | 1 | survey_id | date_created  | date_updated  | opening_date | closing_date | category_id | topic_id |
    | 2 | 33        | Not important | Not important | NULL         | NULL         | 1           | NULL     |
    | 3 | 45        | Not important | Not important | NULL         | NULL         | 6           | NULL     |
    | 4 | 46        | Not important | Not important | NULL         | NULL         | 4           | NULL     |

试试这个查询:

      $my_query="SELECT s.survey_id, s.title,c.categoryname 
               FROM survey_header as sh 
               JOIN survey   AS s ON sh.survey_id=s.survey_id
               JOIN category AS c ON sh.category_id=c.category_id
               WHERE $searchtype LIKE '%".$var."%' 
               ORDER BY title ASC";

      $get_data= mysqli_query($con, $my_query) or die ("Couldn't execute query: ".mysqli_error());

      if (mysqli_num_rows($get_data) > 0 )
      {
         /*create table*/
      }
      else
      // do something else