对数组中找到的每个ID运行单独的查询


Run a separate query for each ID found in the array

尝试对数组中的项运行第二个查询。首先,我创建了一个用户信息数组,然后对于需要查询的每个用户,我要在答案表中找到他们的答案。

这个想法是让数组拥有来自第一个查询的数据,然后简单地将来自答案的数据添加到相关的user元素。下面的代码只列出了第一个系统。

我一直很不情愿把我的问题贴在这里,但是我的一整天都被这个问题占据了,我什么也没得到。提前为我明显有缺陷的方法道歉。

 Array
(
    [0] => Array
        (
            [fname] => asdf
            [lname] => asdf
            [minitial] => a
            [rank] => MAJ
            [uniq] => !s5$qn
            [sysName] = System 1 Name
            [choice] = The choice for This named system
            [priority] = The priority 
            [termcom] = The termcom
            [sysName] = System 2 Name
            [choice] = The choice for This named system
            [priority] = The priority 
            [termcom] = The termcom
            [sysName] = System 3 Name
            [choice] = The choice for This named system
            [priority] = The priority 
            [termcom] = The termcom

   [1] => Array
        (
            [fname] => asdf
            [lname] => lkjlkj
            [minitial] => i
            [rank] => oiuoi
            [uniq] => @z26dr
            [sysName] = System 1 Name
            [choice] = The choice for This named system
            [priority] = The priority 
            [termcom] = The termcom
            [sysName] = System 2 Name
            [choice] = The choice for This named system
            [priority] = The priority 
            [termcom] = The termcom
            [sysName] = System 3 Name
            [choice] = The choice for This named system
            [priority] = The priority 
            [termcom] = The termcom

        )

//代码
$sql = "SELECT fname, lname, minitial, rank, uniq FROM `user` join answers on answers.uniqid = user.uniq";
$data = mysqli_query($con, $sql) or die("MySQL ERROR: ". mysqli_error($con));
$users = array();
$i = 0;
while ($row = mysqli_fetch_array($data, MYSQL_ASSOC))
{
    $users['answers'][$i] = array (
        "fname" => $row['fname'], 
        "lname" => $row['lname'], 
        "minitial" => $row['minitial'], 
        "rank" => $row['rank'], 
        "uniq" => $row['uniq']
     );
    $query2 = "SELECT a.sysid, s.sysName, uniqid, choice, priority, termcom FROM answers a LEFT JOIN systems s ON s.sysID = a.sysid WHERE a.uniqid = '" . $row['uniq'] . "'";
    $data2 = mysqli_query($con, $query2);
    while ($row2 = mysqli_fetch_array($data2, MYSQL_NUM))
    {       
        $users['answers'][$i]['sysName'] = $row2[1];
        $users['answers'][$i]['choice'] = $row2[3];     
    }
    $i++;
}

提前感谢您可能分享的任何见解。

编辑:这是返回的数组,每个用户只列出第一个系统。

[2] => Array
        (
            [fname] => asdf
            [lname] => lkjlkj
            [minitial] => i
            [rank] => oiuoi
            [uniq] => @z26dr
            [sysName] => Super Terminate System
        )
    [3] => Array
        (
            [fname] => Juuu
            [lname] => kjuuu
            [minitial] => k
            [rank] => LTC
            [uniq] => gthdz%
            [sysName] => Super Terminate System
        )

好的当运行脚本时,您不能确切地知道会发生什么(问题是什么)。但是当我看到你的代码时,我可以猜到是哪里出了问题。

首先,我认为你也可以这样做:

$sql = "SELECT * FROM `user` 
        join answers on answers.uniqid = user.uniq
        LEFT JOIN systems s ON s.sysID = a.sysid";

希望这有帮助。否则:

在第二个查询中有一个问题。我认为你应该改变:

$query2 = "SELECT a.sysid, s.sysName, uniqid, choice, priority, termcom FROM answers a LEFT JOIN systems s ON s.sysID = a.sysid WHERE a.uniqid = '" . $row['uniq'] . "'";

$query2 = "SELECT a.sysid, s.sysName, s.uniqid, s.choice, s.priority, s.termcom FROM answers a LEFT JOIN systems s ON s.sysID = a.sysid WHERE a.uniqid = '" . $row['uniq'] . "'";

前面的s = s.choice