…Apache 已经支持 PHP,已配置好PHP 扩展支持 MySQL 数据库,下面为如何在PHP网页中显示本地MySQL数据库的内容:
本地数据库存在一个表:student @test(localhost)
在“C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs”路径下新建一个php文件,内容为:
<!DOCTYPE html>
<html>
<body>
<table style='text-align:left;' border='1'>
<tr><th>SNO</th><th>Name</th><th>Age</th><th>College</th></tr>
<?php
//本地数据库为localhost,root为数据库的账号,666为数据库的密码
$con = mysql_connect("localhost","root","666");
if(!$con){
die(mysql_error());
}
mysql_select_db("test",$con);//连接数据库test
$sql = mysql_query("select * from student");//查询数据表student中的数据
mysql_query("set name utf8");
mysql_query("set college utf8");
$datarow = mysql_num_rows($sql); //长度
for($i=0;$i<$datarow;$i++){ //循环遍历出数据表中的数据
$sql_arr = mysql_fetch_assoc($sql);
$sno = $sql_arr['SNO'];
$name = $sql_arr['Name'];
$age = $sql_arr['Age'];
$college = $sql_arr['College'];
echo "<tr><td>$sno</td><td>$name</td><td>$age</td><td>$college</td></tr>";
}
mysql_close($con);// 关闭数据库
?>
</table>
</body>
</html>
然后在浏览器中输入:http://localhost:8080/sjk3.php
显示为: