gpt4 book ai didi

php - 如何从多个mysql行中获取值?

转载 作者:行者123 更新时间:2023-11-29 18:07:45 25 4
gpt4 key购买 nike

我是 Web 开发新手,刚刚开始使用 php。

情况和我想要做什么:所以我有一个大学项目,其中用户有一个仪表板,并在其下方列出了所有事件和每个事件详细信息。所以在仪表板上我想要这样的东西:

事件名称

事件开始日期

事件说明

事件名称

事件开始日期

事件说明

重复此操作,直到数据库中的所有行都已输出。

我尝试使用此代码:

$eventsquery = ("SELECT * FROM Careerevents JOIN Tickets ON CareerEvents.Event_ID=Tickets.Event_ID JOIN Potential_Employee ON Tickets.User_ID=Potential_Employee.User_ID WHERE Email = '$email'");
$result = mysqli_query($connection, $eventsquery);
$row = mysqli_fetch_assoc($result);

for ($i=0; $i < mysqli_num_rows($result); $i++) {
$eventname = $row['Event_Name'];
$startdate = $row['Start_Date'];
$enddate = $row['End_date'];
$description = $row['Event_Description'];
}

在此之后,我只是在仪表板页面的 html 部分中回显了结果。但只显示第一行。如果我想要显示所有行结果,我该怎么做。

最佳答案

使用$row = mysqli_fetch_assoc($result);只能获取一次单行。

您需要在 while 循环中执行此操作,如下所示:

$eventsquery = ("SELECT * FROM Careerevents JOIN Tickets ON CareerEvents.Event_ID=Tickets.Event_ID JOIN Potential_Employee ON Tickets.User_ID=Potential_Employee.User_ID WHERE Email = '$email'");
$result = mysqli_query($connection, $eventsquery);

// EDIT after your comment 'showing only last row'
// I'd do the following
$events = []; // or $events = Array();
while($row = mysqli_fetch_assoc($result)) {
$events[] = $row; // copy the whole row to a new entry in events-array
//$eventname = $row['Event_Name'];
//$startdate = $row['Start_Date'];
//$enddate = $row['End_date'];
//$description = $row['Event_Description'];
}

// now you have all your events in an array called $events
// and can display them in another loop like so:

foreach($events as $event) {
echo "Name: ".$event['Event_Name'];
}

关于php - 如何从多个mysql行中获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47684070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com