您没有正确关闭PHP
标记。除非您回应,否则您不能将HTML
标记与PHP
代码混合在一起。试想一下:
<?php
$host='localhost';
$username='';
$password='';
$database='reference';
mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($database)or die("cannot select DB");
$sql="SELECT * FROM TestTable";
$result=mysql_query($sql); ?> //<-- close here
<table width="400" border="1" cellspacing="0" cellpadding="3">
<?php // <-- open here
while($rows=mysql_fetch_array($result)){ ?> //<-- close here
<tr>
<td width="30%"><?php echo $rows['firstname']; ?></td>
<td width="30%"><?php echo $rows['lastname']; ?></td>
<td width="30%"><?php echo $rows['gender']; ?></td>
<td width="30%"><?php echo $rows['console']; ?></td>
</tr>
<?php //<-- open here
} ?>
</table>
<?php
mysql_close();
//require_once 'Connection.php'; already connect using above connection
?>
还有一件事,mysql_*
扩展已被弃用,你应该使用mysqli_*
或PDO
代替。
mysqli的版本
<?php
// database connection
$con = mysqli_connect("localhost","","");
mysqli_select_db($con, "reference");
$get_data = "select * from `TestTable`";
$run_data = mysqli_query($con, $get_data);
?>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<?php // <-- open here
while ($rows=mysqli_fetch_array($run_data)) { ?>
<tr>
<td width="30%"><?php echo $rows['firstname']; ?></td>
<td width="30%"><?php echo $rows['lastname']; ?></td>
<td width="30%"><?php echo $rows['gender']; ?></td>
<td width="30%"><?php echo $rows['console']; ?></td>
</tr>
<?php //<-- open here
} ?>
</table>
<?php
mysqli_close();
?>
PDO版本
<?php
// database connection
$con = new PDO('mysql:host=localhost;dbname=reference;charset=utf8', '', '');
$get_data = "select * from `TestTable`";
$run_data = $con->query($get_data);
?>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<?php // <-- open here
while ($rows = $run_data->fetch(PDO::FETCH_ASSOC)) { ?>
<tr>
<td width="30%"><?php echo $rows['firstname']; ?></td>
<td width="30%"><?php echo $rows['lastname']; ?></td>
<td width="30%"><?php echo $rows['gender']; ?></td>
<td width="30%"><?php echo $rows['console']; ?></td>
</tr>
<?php //<-- open here
} ?>
</table>
你是否收到任何错误? –
你有什么错误吗? – Sadikhasan
是的。通过获取网站无法显示页面。 – ganeshvasanth