尝试对数组中的项目运行第二个查询。首先,我创建了一组用户信息,然后对于每个用户,我需要在答案表中查询他们的答案。
这个想法是让数组拥有来自第一个查询的数据,然后简单地将来自答案的数据添加到相关的用户元素。下面的代码仅列出第一个系统。
我一直很不愿意在这里发布我的问题,但是我的一整天都被这个消耗了,而且我没有得到任何快速的地方。提前为我明显有缺陷的方法道歉。
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
)