doudu9148 2015-02-06 13:43
浏览 13

将两个mysqli_fetch_array放入一个数组中

I'm having problem assigning data from second select statment to already existing array: first select works fine and main array gets the data as it should:

$scriptsSolo_SQL = "select * from funkeyword.scripts where pause = '0' AND solo= '1' ";

$result = mysqli_query($conn,$scriptsSolo_SQL);

while ($row = mysqli_fetch_assoc($result)) {
      $mainArray[] = array("script_id" => "$row[id]" , "cron_format" => "$row[cron_format]");
}

but the problem its when im trying to add to $mainArray data from second table :

$usersSolo_SQL = "select * from funkeyword.users where pause = '0' AND solo= '1' ";
$result = mysqli_query($conn,$usersSolo_SQL);
//Get Data from users table
while ($row = mysqli_fetch_assoc($result)) {
    $mainArray[] = array( "user_id" => "$row[id]");
}

how can i add the data from users to same array created from scripts data ??

  • 写回答

1条回答 默认 最新

  • douyabu1528 2015-02-06 13:58
    关注

    a) at the very least add some basic error handling

    $result = mysqli_query($conn, $usersSolo_SQL);
    if ( !$result ) {
        trigger_error('query failed', E_USER_ERROR);
    }
    

    b) (trust me for now, I'll explain later): Don't use SELECT * but SELECT only,the,fields,you,process

    $scriptsSolo_SQL = "select id,cron_format from funkeyword.scripts where pause = '0' AND solo= '1' ";
    $result = mysqli_query($conn,$scriptsSolo_SQL);
    if ( !$result ) {
        trigger_error('query failed', E_USER_ERROR);
    }
    [...]
    $usersSolo_SQL = "select id from funkeyword.users where pause = '0' AND solo= '1' ";
    $result = mysqli_query($conn, $usersSolo_SQL);
    [...]
    

    c) install a debugger, see e.g. https://netbeans.org/kb/docs/php/debugging.html
    Or (if you must) add debug out to check the state(s) of the script (some var_export()s or var_dump()s sometime work miracles. But you have to remove tem afterwards; and it may affect the execution and/or output significantly. printf-debuggers: we all use them; still they are ...suboptimal)

    d) use a UNION statement to fetch both result sets as one. And that's the reason to ask you not to use * in this particular case. You should query UNION result sets only with exactly the same number of fields per record. Hard to tell from *. But with SELECT id,cron_format and SELECT id it's easy to tell "oh, I must come up with another field in the second result set"

    <?php
    $scriptsSolo_SQL = "
        (
            SELECT
                id,cron_format
            FROM
                funkeyword.scripts
            WHERE
                pause = '0' AND solo= '1'
        )
        UNION
        (
            SELECT
                id,null as cronformat
            FROM
                funkeyword.users
            WHERE
                pause = '0' AND solo= '1'
        )
    ";
    
    $result = mysqli_query($conn,$scriptsSolo_SQL);
    if ( !$result ) {
        trigger_error('query failed', E_USER_ERROR);
    }
    while ( $row=mysqli_fetch_assoc($result) ) {
        $mainArray[] = $row;
    }
    

    [ e) does this table structure really, really make sense? It looks a bit strange.... ]

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集