duanna3634 2016-08-02 05:54
浏览 106
已采纳

如何将查询数据放在两个不同的数组中然后在两个表中回显?

Run select query on table

filter the query data

if its gender equal to Man, then put all of his in array men=array()

OR

if its gender is woman then put in array woman=array()

then echo two tables

Men Table

1-Name, age, city, country, etc

2-Name, age, city, country, etc

3-Name, age, city, country, etc . . .

Women Table

1-Name, age, city, country, etc

2-Name, age, city, country, etc

3-Name, age, city, country, etc . . . .

Men table will contain all rows data of Man which are save in Men() array

Women table will contain all rows data of Woman which are save in Women() array

Note:-

I have tried code like this but its mess and i failed:-

https://stackoverflow.com/questions/38653118/notice-undefined-offset-1-putting-mysql-query-data-in-while-loop

  • 写回答

1条回答 默认 最新

  • dongyuduan1890 2016-08-02 06:24
    关注

    your query must be in UNION:

    Select men.Name, men.age, men.city, men.country, 'Male' as 'gender' FROM men_table as men UNION ALL Select women.Name, women.age, women.city, women.country, 'Female' as 'gender FROM women_table as women;
    

    so you can have an query output:

    |Name   |age    |city   |country|gender|
    ----------------------------------------
    |male1  |21     |NY     |USA    |Male  |
    |male2  |23     |CLV    |USA    |Male  |
    |female1|25     |GS     |USA    |Female|
    |female2|27     |CHG    |USA    |Female|
    

    after that you can loop to your result:

    <?php 
    
    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");
    
    /* check connection */
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s
    ", $mysqli->connect_error);
        exit();
    }
        $query = "Select men.Name, men.age, men.city, men.country, 'Male' as 'gender' FROM men_table as men UNION ALL Select women.Name, women.age, women.city, women.country, 'Female' as 'gender FROM women_table as women";
    
        $men = array();
        $women = array;
    
        if ($result = $mysqli->query($query)) {
    
            /* fetch associative array */
            while ($row = $result->fetch_assoc()) {
                if($row["gender"] == 'Male') {
                    $men[] = $row[];
                }
                else {
                    $women[] = $row[];
                }
            }
    
            print_r($men);
            print_r($women);
    
            /* free result set */
            $result->free();
        }
    
    /* close connection */
    $mysqli->close();
    ?>
    

    The output will be like:

    //$men array
        array(
            [0] => array(
                    'Name' => 'male1', 
                    'age' => '21', 
                    'city' => 'NY', 
                    'country' => 'USA', 
                    'gender' => 'Male'
            ),
            [1] => array(
                    'Name' => 'male2', 
                    'age' => '23', 
                    'city' => 'CLV', 
                    'country' => 'USA', 
                    'gender' => 'Male'
            )
        );
    
    //$women array
        array(
            [0] => array(
                    'Name' => 'female1', 
                    'age' => '25', 
                    'city' => 'GS', 
                    'country' => 'USA', 
                    'gender' => 'Female'
            ),
            [1] => array(
                    'Name' => 'female2', 
                    'age' => '27', 
                    'city' => 'CHG', 
                    'country' => 'USA', 
                    'gender' => 'Female'
            )
        );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题