这是我的代码:
<?php
if (isset($_GET['query']))
$query = $_GET['query'];
$min_length = 3;
if (isset($_GET['query']))
if(strlen($query) >= $min_length){
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM studentsroom
WHERE (`room` LIKE '%".$query."%') OR (`name` LIKE '%".$query."%') OR (`studentId` LIKE '%".$query."%') ORDER BY room") or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){
while($results = mysql_fetch_array($raw_results)){
echo '<table border="1" cellspacing="1">';
'<td width="100"><strong> Room: </strong>'.$results['room'].'</td>
<td width="200"><strong> Student ID: </strong>'.$results['studentId'].'</td>
<td width="250"><strong> Name: </strong>'.$results['name'].'</td>';
echo '</table>';
}
}
else{
echo "No results";
}
}
else{
echo "Minimum length is ".$min_length;
}
?>
我希望结果显示在表格中,但两者之间没有间距,如下所示:
有人可以帮助我为表格中显示的结果提供更好的HTML代码吗?
答案 0 :(得分:1)
在循环外部获取表标记,并为每个条目生成一个新行:
if(mysql_num_rows($raw_results) > 0){
echo '<table border="1" cellspacing="1">';
while($results = mysql_fetch_array($raw_results)){
echo '<tr>
<td width="100"><strong> Room: </strong>'.$results['room'].'</td>
<td width="200"><strong> Student ID </strong>'.$results['studentId'].'</td>
<td width="250"><strong> Name: </strong>'.$results['name'].'</td>
</tr>';
}
echo '</table>';
}
编辑:还要注意mysql函数是deprecated,切换到mysqli或PDO。
答案 1 :(得分:1)
试试这个
echo '<table border="1" cellspacing="1">';
while($results = mysql_fetch_array($raw_results)){
echo '<tr>';
echo '
<td width="100"><strong> Room: </strong>'.$results['room'].'</td>
<td width="200"><strong> Student ID:</strong>'.$results['studentId'].'</td>
<td width="250"><strong> Name: </strong>'.$results['name'].'</td>';
echo '</tr>';
}
echo '</table>';