使用3个变量获取大量数据 [英] Using 3 Variables for a multitude of data

查看:69
本文介绍了使用3个变量获取大量数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(标题有点奇怪,可以随意更改,我不知道该怎么写) HTML表将是MySQL表中最后5件事的历史记录,当加载第一行时,它存储在HTML表中第一行的变量中,然后加载/获取下一个MYSQL行,并且然后,将HTML表中的第二行更改为MYSQL表的第二行.

(The title is a bit weird, feel free to change it, I didn't know what to put) A HTML table will be a history of the last 5 things in a MySQL table, when the first row is loaded, it is stored in a variable for the first row in the HTML table, then the next MYSQL row is loaded/fetched and the second row in the HTML table is then changed to the second row of the MYSQL Table.

示例:

for each row:
$username = row["username"]
$name = row["name"]

最终结果应在HTML表中: 这些使用上面的变量,根据MySQL表的最后5个表,我需要它们都是不同的数据

End Result should be in the HTML table: These are using the above variables, and I need them to be all different data according to the last 5 tables in the MySQL Table

用户名 一个Name1(MySQL第1行) 两个Name2(MySQL第2行) 三个Name3(MySQL第3行) 四个Name4(MySQL第4行)

Username Name One Name1 (MySQL Row 1) Two Name2 (MySQL Row 2) Three Name3 (MySQL Row 3) Four Name4 (MySQL Row 4)

到目前为止的代码:

$stmt = $dbConnection->prepare("SELECT * FROM history ORDER BY id DESC LIMIT 5");
$stmt->execute();



foreach ($stmt as $row) {
    $username = $row['username'];
    $gamemode = $row['gamemode'];
    $winnings = $row['won'];
}

推荐答案

要使代码尽可能接近,可以使用数组来存储数据,如下所示:

To keep the code as close as you have it, you could use arrays to store the data, like this:

$stmt = $dbConnection->prepare("SELECT * FROM history ORDER BY id DESC LIMIT 5");
$stmt->execute();

$usernames = array();
$gamemodes = array();
$winnings = array();

foreach ($stmt as $row) {
    $usernames[] = $row['username'];
    $gamemodes[] = $row['gamemode'];
    $winnings[] = $row['won'];
}

然后,您可以像这样访问它们:

Then, you could access them like this:

echo $usernames[1]; // Second name

这篇关于使用3个变量获取大量数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆