[MySQL的]
我有两张桌子
其中一个是这样的:
user_id
| admin_id
| ip
| date
| unbandate
| reason
411
| 593
| 127.0.0.1
| 1404767331
| 1405026531
| reason 1
第二个:
ID
| Name
|
411
| Danny
|
现在,我需要在html表中显示第一个MySql表中的数据。但是,我没有显示id,而是需要第二个表中该id的名称。
这是我目前的代码,它只是从第一个MySql表中检索数据。
<html>
<head>
<title>test</title>
<link href="table.css" rel="stylesheet" type="text/css" media="all">
<meta charset="Windows-1252">
</head>
<center>
<table id="Messages" class="simple-little-table" style="width: 50%;">
<TR><TD><b><font color="000000">Nick</font></TD><TD><b><font color="000000">Admin nick</font></TD><TD><b><font color="000000">IP</font></TD><TD><b><font color="000000">Bandate</font></TD><TD><b><font color="000000">Unbandate</font></TD><TD><b><font color="000000">Reason</font></TD></TR>
<?php
define('DB_HOST' , 'localhost');
define('DB_USER' , 'root');
define('DB_PASSWORD' , '');
define('DB_NAME' , 'ucp1');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$query = "SELECT `user_id`,`admin_id`,`ip`,`date`,`unbandate`,`reason` FROM bans";
$result = mysqli_query($dbc,$query);
while($row = mysqli_fetch_array($result)) {
$nick = $row['user_id'];
$whobanned = $row['admin_id'];
$ip = $row['ip'];
$bandate = $row['date'];
$unbandate = $row['unbandate'];
$reason = $row['reason'];
print '<TR><TD><b>'. $nick .'</b></TD> <TD><b>' . $whobanned . '</b></TD><TD><b>'. $ip .'</b></TD> <TD><b>'. gmdate("Y/m/d", $bandate) .'</b></TD> <TD><b>'. gmdate("Y/m/d", $unbandate) .'</b></TD><TD><b>'. $reason .'</b></TD></TR><br />';
}
?>
</TABLE></center>
</html>
答案 0 :(得分:0)
您需要加入两个表
SELECT `user_id`,`admin_id`,`ip`,`date`,`unbandate`,`reason`,`Name` FROM bans JOIN table2 ON table2.ID = bans.user_id
http://www.sitepoint.com/understanding-sql-joins-mysql-database/