0

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

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

我一直很不愿意在这里发布我的问题,但是我的一整天都被这个消耗了,而且我没有得到任何快速的地方。提前为我明显有缺陷的方法道歉。

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

1 回答 1

1

好的,您并没有确切地知道运行脚本时会发生什么(问题是什么)。但是当我看到你的 coude 时,我可以假设出了什么问题。

首先,我认为你也可以通过查询来做到这一点:

$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

于 2013-01-11T21:12:57.113 回答