いくつかの結合を含む 1 つの選択クエリを使用して mysql テーブルからデータを取得する際に、興味深い問題に遭遇しました。
1)クエリ:
$task_details = "SELECT tasks.task, ";
$task_details = $task_details . "tasks.description,";
$task_details = $task_details . "tasks.finishby, ";
$task_details = $task_details . "responsibles.full_name, ";
$task_details = $task_details . "task_assignments.completed, ";
$task_details = $task_details . "tasks.id, ";
$task_details = $task_details . "responsibles.user_id ";
$task_details = $task_details . "FROM tasks,task_assignments,responsibles ";
$task_details = $task_details . "WHERE ";
$task_details = $task_details . "tasks.id = task_assignments.id_task AND ";
$task_details = $task_details . "responsibles.id = task_assignments.id_assignee AND ";
$task_details = $task_details . "tasks.id = $id_task;";
$task_details_q = mysql_query($task_details) or die(mysql_error());
1a)結果のクエリ例:
SELECT tasks.task, tasks.description, tasks.finishby, responsibles.full_name, task_assignments.completed, tasks.id, responsibles.user_id
FROM tasks, task_assignments, responsibles
WHERE tasks.id = task_assignments.id_task
AND responsibles.id = task_assignments.id_assignee
AND tasks.id =19
2) HTML / PHP コード:
<table class="task_table">
<thead>
<th>Task</th>
<th>Description</th>
<th>Due date</th>
<th>Person</th>
<th>Completed</th>
</thead>
<?php
$even = false;
$trow = "";
while($row = mysql_fetch_array($task_details_q))
{
$trow = $trow . "<tr";
if($even) $trow = $trow . " style=\"background-color: #f2f2ed; \"";
$trow = $trow. ">";
$trow = $trow . "<td >$row[0]</td>";
$trow = $trow . "<td>" . $row[1] . "</td>";
$trow = $trow . "<td>" . date('d-m-Y',$row[2]) . "</td>";
$trow = $trow . "<td>$row[3]</td>";
$trow = $trow . "<td style=\"text-align: center;\" >";
if($row[4] > 0)
{
$trow = $trow . "<a href=\"javascript:modifyCompleted('remove','$row[6]',$row[5])\" title=\"Click to change completion of this task by this person\">yes</a>";
}
else
{
$trow = $trow . "<a href=\"javascript:modifyCompleted('add','$row[6]',$row[5])\" title=\"Click to change completion of this task by this person\">no</a>";
}
$trow = $trow . "</td>";
$trow = $trow . "</tr>";
$even =! $even;
$number = $number + 1;
}
$trow = $trow . "<tr style=\"border-top: 1px solid #666666;\"><td></td><td></td><td></td><td></td>";
$trow = $trow . "<td>";
$trow = $trow . "<a href=\"javascript:modifyCompleted('add_all','all',$task_details_array[5])\" title=\"Click to complete all\">Complete all</a>";
echo $trow;
?>
</table><br />
<span style="text-align: center;display:block;font-size: 12px;"><a href="tasks.php">Go back to task overview</a></span>
3)問題 / 質問: 何らかの理由で、表示されたテーブルが常に1 つのレコードを省略しています。多くの PHP スクリプトで同じ (または非常に類似した概念) を使用しましたが、同じ問題に遭遇したことはありません。クエリ自体は問題ではないと思います-DBに対して直接実行すると、正しい数の値が返されます...(私は思う)。