需求
- php连接数据库
- 查询遍历所有数据
PHP连接数据库
//config.php文件内容
$database = "xx";
$dataname = "root";
$datapasswd = "xxx";
$datalocal = "localhost:3306";
//$dataport = "3306";
$con = mysqli_connect($datalocal,$dataname,$datapasswd,$database);
if(!$con){
echo "数据库连接异常";
}
//设置查询数据库编码
mysqli_query($con,\'set names utf8\');
查询遍历所有数据
<?php
//连接数据库
include \'./inc/config.php\';
//查询数据库
$query = \'select * from user\';
$result1 = mysqli_query($con,$query);
//函数返回结果集中行的数量
if(mysqli_num_rows($result1)>0){
//mysqli_fetch_array以数组的形式返回,关联数组
while($row = mysqli_fetch_array($result1)){
echo
"<center>
<table>
<tr>
<th>账号</th>
<th>密码</th>
</tr>
<tr>
<td>".$row[\'id\']."</td>
<td>".$row[\'name\']."</td>
<td>".$row[\'password\']. "</td>
</tr>
</table>
</center>";
}
}
//关闭数据库
mysqli_close($con);
?>