我有问题......
我有一个名为事件参与的表,我们将它们记录到具有ID的表中。事件的ID和用户ID。
当我想列出参加的活动时,我需要从用户表中带来用户,并带上姓名,电话和电子邮件。
但是当我编写代码时,ID结果为true。但是当我把第二个SQL放在循环中时,代码只给第一个记录带来了另一个空值。
任何人都可以帮助我......
我是PHP的初学者所以我有错误:(
// START TABLE CONNECTIONS //
$result_event_attends = mysql_query("SELECT * FROM event_attends where course_id='" .$val_selected_event_id. "' ORDER BY idcourse_attends DESC");
$total_results_event_attends = mysql_num_rows($result_event_attends);
$total_pages_event_attends = ceil($total_results_event_attends / $per_page);
// END TABLE CONNECTIONS //
// PAGINATION START //
// check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// PAGINATION END //
// START TO GET RESUTLS //
// CHECK IF THE RESULTS 0 AND GIVE ERROR //
$num_rows_event_attends = mysql_num_rows($result_event_attends);
if($num_rows_event_attends == 0)
{
echo "<div class='alert alert-error'>";
echo "<center><strong>Oh snap!</strong> There are no records. Do you want add records?</center>";
echo "</div>";
}
// END CHECK IF THE RESULTS 0 AND GIVE ERROR //
else {
echo "<BR><table class='table table-striped table-bordered table-condensed'>";
echo "<tr>";
echo '<th style="vertical-align:middle"><center>Attender Name</center></td>';
echo '<th style="vertical-align:middle"><center>Registration No</center></td>';
echo '<th colspan="4"><center>Actions</center></td>';
echo "</tr>";
for ($i = $start; $i < $end; $i++)
{
// make sure that PHP doesn't try to show results that don't exist
$user_id = mysql_result($result_event_attends, $i, 'user_id');
$user_info = mysql_query("SELECT * FROM users where userID='" .$user_id. "'");
$total_results_user_info = mysql_num_rows($user_info);
if ($i == $total_results_event_attends) { break; }
// echo out the contents of each row into a table
echo "<tr>";
echo '<td style="vertical-align:middle">' . mysql_result($user_info, $i, 'name') . '</td>';
echo '<td style="vertical-align:middle">' . mysql_result($result_event_attends, $i, 'company_id') . '</td>';
echo '<td width="40px"><center><a href="events.php?action=delete_conf&id=' . mysql_result($result, $i, 'EV_ID') . '" class="btn btn-mini btn-info" title="Attendes List"><i class="icon-group"></i></a></center></td>';
echo '<td width="40px"><center><a href="events.php?action=delete_conf&id=' . mysql_result($result, $i, 'EV_ID') . '" class="btn btn-mini btn-info" title="Delete"><i class="icon-trash-o"></i></a></center></td>';
echo '<td width="40px"><center><a href="events.php?action=edit&id=' . mysql_result($result, $i, 'EV_ID') . '" class="btn btn-mini btn-info" title="Edit"><i class="icon-edit"></i></a></center></td>';
echo '<td width="40px"><center><a href="events.php?action=details&id=' . mysql_result($result, $i, 'EV_ID') . '" class="btn btn-mini btn-info" title="Details"><i class="icon-bars"></i></a></center></td>';
echo "</tr>";
}
// close table>
echo "</table>";
// display pagination
echo "<br />";
echo "<div class='pagination pagination-centered'>";
echo "<ul> ";
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<li><a href='?page=$i'>$i</a></li> ";
}
echo "</ul> ";
echo "</div> ";
}
答案 0 :(得分:1)
首先,我强烈建议您使用mysqli
,而不是当前正在使用的mysql
功能。
您似乎做得有些低效,因为您在循环的每次迭代中都要查询数据库。考虑获取所有数据库结果并将其转换为多维数组(如果您决定使用mysqli_fetch_all
)然后循环遍历该数组的第一维并执行任何验证,那么mysqli
将是一个良好的开端然后
此外,您可以考虑使用SQL联接来合并数据。你的代码有点不清楚,所以我不完全确定这些选项中的任何一个都是有效的(也是我为什么不提供例子的原因)。
答案 1 :(得分:0)
对于查询,从一开始就学会有效地使用数据库会更好。你所拥有的是一系列有效的查询循环。
这就是你现在所拥有的:
SELECT *
FROM event_attends
where course_id='$val_selected_event_id'
ORDER BY idcourse_attends DESC
;
/* inefficiently this is repeated for each row from the first query */
SELECT *
FROM users
where userID = '$user_id'
;
您可以使用单个查询:
获得相同的结果SELECT ca.* , u.*
FROM event_attends ca
INNER JOIN users u ON ca.user_id = u.user_id
where ca.course_id='$val_selected_event_id'
ORDER BY ca.idcourse_attends DESC
小心select *
;它不应该用在生产代码中。您应该列出实际需要的字段,这样可以最大限度地减少收集数据并将数据传输到应用程序的工作。
另请注意使用ca
和u
table aliases
为您引用的每个字段添加前缀,以便dbms知道正在引用哪个字段。