I'm writing a code for my little admin panel, and since I'm not that advanced of a coder, I'm experiencing some troubles with getting a name using two different tables.
我正在为我的小管理员面板编写代码,因为我不是那么高级的编码器,我在使用两个不同的表获取名称时遇到了一些麻烦。
Here's my code so far:
到目前为止,这是我的代码:
<?php
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
include 'db_connect.php';
$sql = "SELECT * FROM $tbl_name WHERE is_dead='0'";
$result=mysql_query($sql);
?>
<title>Title</title>
<center><img src="header.png"></center>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<? include 'menu.php';?>
</tr>
<tr>
<td align="center"><strong>ID</strong></td>
<td align="center"><strong>Unique ID</strong></td>
<td align="center"><strong>Model</strong></td>
<td align="center"><strong>Last Online</strong></td>
<td align="center"><strong>Options</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['id']; ?></td>
<td><? if ($rows['unique_id'] == 7815684) { echo '<font color="blue"><b><u>7815684</u></b></font>'; }
elseif ($rows['unique_id'] == 2312964) { echo '<font color="blue"><b><u>2312964</u></b></font>'; }
else { echo $rows['unique_id']; } ?></td>
<td><? echo $rows['model']; ?></td>
<td align='center'><font color="green"><b><? echo $rows['last_updated']; ?></b></font></td>
<td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update</a>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
So what I'm trying to do is to get user name, using two tables $tbl_name
and $prtalbe
using their unique_id. So, if unique_id from $tbl_name
equals unique_id from $prtable
, I want to show user's name from $prtalbe
.
所以我要做的是获取用户名,使用两个表$ tbl_name和$ prtalbe使用他们的unique_id。因此,如果$ tbl_name中的unique_id等于$ prtable中的unique_id,我想显示来自$ prtalbe的用户名。
I've been trying another sql query:
我一直在尝试另一个SQL查询:
$sql = "SELECT * FROM $tbl_name, $prtable WHERE $tbl_name.unique_id = $prtable.unique_id;
$result=mysql_query($sql);
Then doing while loop to get it working
然后做while循环以使其工作
while($rows=mysql_fetch_array($result)){
$rows['name'];
}
and it did work actually, but it didn't want to put it right into my code, since ID
from $prtable
and $tbl_name
are different.
它实际上确实有效,但它不想把它直接放到我的代码中,因为来自$ prtable和$ tbl_name的ID是不同的。
1 个解决方案
#1
0
Try this:
$sql = "SELECT $prtable.username FROM $tbl_name INNER JOIN $prtable ON ($tbl_name.unique_id = $prtable.unique_id)";
When you call INNER JOIN
you are fetching all rows from each table and combining them given the ON
condition. For more information, see this: http://www.w3schools.com/sql/sql_join_inner.asp
当您调用INNER JOIN时,您将从每个表中获取所有行,并在给定ON条件的情况下组合它们。有关详细信息,请参阅:http://www.w3schools.com/sql/sql_join_inner.asp
#1
0
Try this:
$sql = "SELECT $prtable.username FROM $tbl_name INNER JOIN $prtable ON ($tbl_name.unique_id = $prtable.unique_id)";
When you call INNER JOIN
you are fetching all rows from each table and combining them given the ON
condition. For more information, see this: http://www.w3schools.com/sql/sql_join_inner.asp
当您调用INNER JOIN时,您将从每个表中获取所有行,并在给定ON条件的情况下组合它们。有关详细信息,请参阅:http://www.w3schools.com/sql/sql_join_inner.asp