我是网络开发的新手,刚开始使用php。
情况和我正在尝试做的事情:所以我有一个uni项目,用户有一个仪表板,并且所有事件和每个事件详细信息都列在neath下面。所以在仪表板上我想要这样的东西:
活动名称
活动开始日期
活动说明
活动名称
活动开始日期
活动说明
重复,直到输出数据库中的所有行。
我试过使用这段代码:
public sealed class MyType
{
public MyType()
{
_initializationTask = InitializeAsync();
}
private Task _initializationTask;
private async Task InitializeAsync()
{
// Asynchronously initialize this instance.
_customers = await LoadCustomersAsync();
}
public async Task<Customer> LookupCustomer(string name)
{
// Waits to ensure the class has been initialized properly
// The task will only ever run once, triggered initially by the constructor
// If the task failed this will raise an exception
// Note: there are no () since this is not a method call
await _initializationTask;
return _customers[name];
}
// one way of clearing the cache
public void ClearCache()
{
InitializeAsync();
}
// another approach to clearing the cache, will wait until complete
// I don't really see a benefit to this method since any call using the
// data (like LookupCustomer) will await the initialization anyway
public async Task ClearCache2()
{
await InitializeAsync();
}
}
在此之后,我只是在仪表板页面的html部分回应了结果。但只有第一行显示出来。如果我想要显示所有行结果,我该怎么做呢?
答案 0 :(得分:0)
当你这样做时
$row = mysqli_fetch_assoc($result);
你拉第一排。所以当你试图抓住一个值
时$eventname = $row['Event_Name'];
你总是拉同一个变量。您永远不会使用新值更新$row
,因此您总是会返回相同的值。这就是为什么人们通常使用while循环,将定义作为循环的条件;只要它有效(例如,它拉一行),它就会继续循环。
while ($row = mysqli_fetch_assoc($result)) {
那就是说,我建议你学习PDO而不是使用mysqli。 PDO更现代,更安全,你会遇到更多,并且在功能上几乎与mysqli相同。如果你已经对mysqli感到满意,那么从一个学习的角度来看,它并不是一个很大的跳跃,那就是FAR领先。
答案 1 :(得分:0)
您只能使用$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'];
}