我的代码是显示给我只是第一个结果,并带来另一个null


my code is show to me just first result and bring the other null

我有一个问题…

我有一个名为event出席的表,我们用ID将它们记录到表中。事件ID和用户ID。

当我想要列出事件出席时,我需要从用户表中取出用户,并带来姓名,电话和电子邮件。

,但当我写代码的ID结果为真。但是当我将第二个SQL放入循环时,代码只给我带来第一个记录,并带来另一个null。

谁能帮我一下……

我是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> ";
}

在此之前,我强烈建议您使用mysqli而不是当前使用的mysql函数。

你似乎在做一些效率不高的事情,因为你在每次循环迭代时都在查询数据库。考虑将所有数据库结果转换为多维数组(如果决定使用mysqli, mysqli_fetch_all将是一个良好的开端),然后循环遍历该数组的第一个维度,然后执行任何验证。

还可以考虑使用SQL连接来合并数据。您的代码有点不清楚,所以我不完全确定这些选项中的任何一个是有效的(也是为什么我不愿意提供示例)。

对于查询,最好从一开始就学习如何有效地使用数据库。你所拥有的是一个完全没有效率的查询循环。

这是你现在的文件:

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 *;它不应该在产品代码中使用。你应该列出你真正需要的字段,这样你可以最大限度地减少收集和传输数据到你的应用程序的工作。

还要注意cau的使用,它们是table aliases前缀您引用的每个字段,因此dbms将知道哪个字段被引用。