[英]How can I get this SQL query to work…?
I am making a game for class and I decided to make a leaderboards page. 我正在为课堂制作游戏,因此我决定制作排行榜页面。 As I have an admin area too I need to grab data from both tables in the page. 由于我也有一个管理区域,因此我需要从页面中的两个表中获取数据。
The two tables are user_settings
and leaderboards
. 这两个表是user_settings
和leaderboards
。 This is the code on my leaderboards.php page: 这是我的Leaderboards.php页面上的代码:
<?php $records = array();
if ($results = $db->query("SELECT * FROM user_settings UNION SELECT * FROM leaderboards")) {
if ($results->num_rows) {
while ($row = $results->fetch_object()) {
$records[] = $row;
}
$results->free();
}
}
?>
<?php foreach ($records as $data) { ?>
...the HTML etc...
<?php } ?>
What do I need to change in order to make this query work. 为了使此查询有效,我需要更改什么。 Do I need to change the foreach loop? 我需要更改foreach循环吗? or...? 要么...? I have only so far needed to grad data from one table so I'm not sure how to get this to work. 到目前为止,我只需要从一个表中对数据进行分级,所以我不确定如何使它起作用。
EDIT: 编辑:
The user_settings
table is for admin set config such as the site name etc. So far all I am doing is trying to do is select all from two tables without the page breaking, at the moment it is breaking. user_settings
表用于管理设置配置,例如站点名称等。到目前为止,我要做的就是从两个表中选择所有内容,而不会中断页面。 I have a feeling it is to do with the foreach loop and some of the code underneath the query. 我感觉这与foreach循环以及查询下面的一些代码有关。 At the moment I do not want to grab any data from the database, I just want to select all successfully so the page doesn't break. 目前,我不想从数据库中获取任何数据,我只想成功选择所有数据,以使页面不会中断。
As @Kai Qing stated you are using UNION
. 正如@Kai Qing所说,您正在使用UNION
。 As I don't know what your table structures are I cannot guarantee this is what you are after however I believe you are wanting to select all from two tables that do not share the same structure therefore you will want to do: 因为我不知道您的表结构是什么,所以不能保证这就是您想要的,但是我相信您希望从两个不共享相同结构的表中选择所有表,因此您需要执行以下操作:
if ($results = $db->query("SELECT * FROM user_settings, leaderboards")) {
I hope this helps. 我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.