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

时间:2013-01-11 20:16:44

标签: php mysql arrays

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

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

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

 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
        )

1 个答案:

答案 0 :(得分:1)

O.K。你没有确切地告诉你在运行脚本时会发生什么(问题是什么)。但是当我看到你的歌曲时,我可以假设出了什么问题。

首先,我认为您也可以使用查询:

$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。在choice = s.choice之前