嘗試運行數組中的項目的第二個查詢。 首先,我創建了一組用戶信息,然後爲每個用戶查詢他們在答案表中找到的答案。爲陣列中的每個ID運行單獨的查詢
這個想法是爲數組提供來自第一個查詢的數據,然後簡單地將答案中的數據添加到相關的用戶元素。下面的代碼只列出了第一個系統。
我一直都很不願意在這裏發表我的問題,但是我的整個一天都被這個消耗掉了,我沒有得到快速的地方。對於我明顯有缺陷的方法,事先抱歉。
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
)
// CODE
$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語句來返回更接近最終結果集的東西?如果你無法獲得連接,甚至可以使用一些子選擇。這可能是一個更簡單的方法。 – ficuscr
另外,您遇到的問題是什麼? – crush
@ goodmood2那麼,我認爲有http://codereview.stackexchange.com/? –