[英]Displaying a result from SQL inner join table on php page
So i have two tables in my database: 所以我的数据库中有两个表:
Users: 用户:
userID usertype username userID usertype用户名
Ticket: 票:
ticketID userID ticketMessage ticketID userID ticketMessage
My goal is to echo with php the username from the USER table where the USERID from ticket = the same USERID as the one in the USER table. 我的目标是用php回显来自USER表的用户名,其中来自ticket的USERID =与USER表中的USERID相同的USERID。
Here is my code so far: 到目前为止,这是我的代码:
<?PHP
if ($result['admin'] == 1) {
$sql = "SELECT * FROM ticket ORDER BY ticketDateTime DESC";
$res = do_sql($sql);
if ($res->rowCount() == 0) {
echo "You have 0 Tickets, you haven't created any tickets!";
} else {
?>
<table class="table-container">
<thead>
<tr>
<th id="table-subject">Subject</th>
<th>Id</th>
<th>Created</th>
<th>User</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
?>
<tr>
<td>
<a id="ticket-subject" href="single-ticket-view.php?page=singletickview&ticketID=<?php echo $row['ticketID'];?>">
<?php
echo $row['ticketSubject'];?>
</a>
</td>
<td><?php
echo sprintf('%04d', $row['ticketID']); ?>
</td>
<td><?php
$a_date = strtotime($row['ticketDateTime']);
$formatted_date = date( 'd/m/Y', $a_date );
echo $formatted_date; ?>
</td>
<td>
<?php
$sql2 = "SELECT users.username FROM users INNER JOIN ticket ON users.userID = '" . $row['userID'] . "'";
$res2 = do_sql($sql2);
if ($res2->rowCount() == 1) {
echo $res2['username'];
}
?>
</td>
<td>
<?php if ($row['is_active'] == 1) {?>
<div class="status" id="open">Open</div>
<?php } else { ?>
<div class="status" id='closed'>Closed</div> <?php } ?>
</td>
</tr>
</tbody>
<?php
}}?>
</table>
My problem here is that i cant figure out how to echo the username on line 60, i think it has somthing to do with my SQL query. 我的问题是,我无法弄清楚如何在第60行回应用户名,我认为它与我的SQL查询有关。 Here is my SQL query and php code from line 60 in above section of code - The problem 100% lies within this section, Maybe to do with Query and improper way of using the INNER JOIN.
这是我的SQL查询和代码上面的第60行的PHP代码 - 问题100%在本节内,可能与查询和使用INNER JOIN的不正确方式有关。 here it is again:
这里又是:
<td>
<?php
$sql2 = "SELECT users.username FROM users INNER JOIN ticket ON users.userID = '" . $row['userID'] . "'";
$res2 = do_sql($sql2);
if ($res2->rowCount() == 1) {
echo $res2['username'];
}
?>
</td>
$row['userID'] = the userID from table ticket which happens to be 11 in this instance. $ row ['userID'] =来自表票证的userID,在这个例子中恰好是11。 I went to the Run SQL query in PHP my admin and ran the query:
我在PHP管理员中运行了运行SQL查询并运行了查询:
SELECT users.username FROM users INNER JOIN ticket ON users.userID = 11
which returned the correct result i am looking for, so i have no idea why it will not echo on my PHP webpage. 它返回了我正在寻找的正确结果,所以我不知道为什么它不会在我的PHP网页上回显。 Any help would be amazing, thanks!
任何帮助都会很棒,谢谢!
EDIT: 编辑:
I forgot to show my do_sql(); 我忘了展示我的do_sql(); function, here it is:
功能,这里是:
function do_sql($sql_string) {
$connection = new PDO("mysql:host=localhost;dbname=secret", 'root', '');
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$row_result = $connection->query($sql_string);
} catch (PDOException $Exception) {
echo $sql_string . '</br>';
echo $Exception;
exit();
}
return $row_result;
} }
Instead of call query multiple times you can Join query and get user name with single query execution. 多次调用查询,您可以通过单个查询执行加入查询并获取用户名。 change this line
改变这一行
$sql = "SELECT * FROM ticket ORDER BY ticketDateTime DESC";
to 至
$sql = "SELECT ticket.*,Users.username FROM ticket LEFT JOIN Users ON ticket.userID = Users.userID ORDER BY ticketDateTime DESC";
You can get user name by $row['username']
; 您可以通过
$row['username']
获取用户名; in loop 在循环中
如果您只是在初始查询中执行连接,那么您将可以访问所需的所有字段,而无需运行第二个查询。
SELECT * FROM ticket INNER JOIN users ON users.userID = ticket.userID ORDER BY ticketDateTime DESC
应该是SELECT users.username FROM users INNER JOIN ticket ON users.userID = ticket.USERID
希望它SELECT users.username FROM users INNER JOIN ticket ON users.userID = ticket.USERID
帮助,如果您选择特定记录,请在查询中添加WHERE
子句
You only need one SQL query to get all the data that you need. 您只需要一个SQL查询即可获得所需的所有数据。
SELECT Ticket.*, User.username FROM Ticket INNER JOIN User ON User.userid = Ticket.userID;
This will select all the ticket data, along with the username of the user that the ticket belongs to. 这将选择所有票证数据以及票证所属的用户的用户名。 Usage example:
用法示例:
echo $data['username'] . ' has a ticket with the following message: ' . $data['ticketMessage'];
In the future, I recommend you to prefix your column names to prevent confusion with queries like these. 将来,我建议您为列名添加前缀,以防止与这些查询混淆。 (eg user_name, user_id, ticket_message, ticket_id).
(例如user_name,user_id,ticket_message,ticket_id)。
You can read up more about INNER JOIN here. 您可以在此处阅读有关INNER JOIN的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.