我有一個while循環,應該首先從一個名爲submittedresume使用從另一個函數獲取的job_id individual_id
表。然後它將在另一個while循環中使用該id從各個表中獲取first_name
和last_name
。然後它將使用另一個while循環從提交的恢復表中獲取submitted_id
。雖然嵌套循環不返回正確的輸出
我分割第一個和最後一個while循環以從輸出中獲取不同的值。
我第一次while循環。它首先使用從其他函數獲取的job_id從名爲submittedresume的表中獲取individual_id
。它給了我兩個用戶ID的正確輸出:9和4.
global $database;
$query = "SELECT DISTINCT individual_id FROM submittedresume WHERE job_post_id='$id'";
$output = $database->query($query);
while ($row = mysqli_fetch_array($output)) {
$indvId = $row[0];
}
這是第二個內部while循環。它給了我瑪麗珍(不再重複)和湯姆索耶的輸出。
$indvId = $row[0];
$sql = "SELECT * FROM individual WHERE individual_id='$indvId'";
$result = $database->query($sql);
while ($details = mysqli_fetch_array($result)) {
$first = $details['first_name'];
$last = $details['last_name'];
echo $first;
echo $last;
}
這是我的全部功能:
public function displayApplications($id){
global $database;
$query = "SELECT DISTINCT individual_id FROM submittedresume WHERE job_post_id='$id'";
$output = $database->query($query);
while ($row = mysqli_fetch_array($output)) {
$indvId = $row[0];
$sql = "SELECT * FROM individual WHERE individual_id='$indvId'";
$result = $database->query($sql);
while ($details = mysqli_fetch_array($result)) {
$first = $details['first_name'];
$last = $details['last_name'];
$sqlquery = "SELECT DISTINCT resume_title FROM submittedresume WHERE individual_id='$indvId' order by submitted_id";
$data = $database->query($sqlquery);
if (mysqli_num_rows($data) == 0) {
echo "Database is empty <br>";
} else {
while (($name = mysqli_fetch_array($data))) {
echo $first . " " . $last . " "."<a href=handleDownload.php?id=$id>$name[0]</a><br>";
}
}
}
}
}
這就是我得到現在:
first_name | last_name | resume_name
Mary | Jane | resume_1
Mary | Jane | resume_2
這就是我在尋找:
first_name | last_name | resume_name
Mary | Jane | resume_2
Tom | Sawyer | resume_1
你需要重寫你的查詢和使用'JOIN'代替 – CodeGodie