在PHP中获取/放置mysqli_num_row和mysqli_fetch_array中的2个变量

时间:2017-01-09 13:16:31

标签: php arrays search display

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

<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>

这是表类别(categoryname是我需要的)

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

这是表格调查(标题是我所需要的)

| 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           |  

这是表survey_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     |

1 个答案:

答案 0 :(得分:1)

尝试此查询:

      $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