<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$dsn = 'mysql:dbname=test; host=127.0.0.1; port=3306; charset=utf8';
$user = 'root' ;
$password = '' ;
try {
// 创建对象(已经包含了连接了,同时也确定了数据库,还同时设置了字符集)
$dbh = new PDO($dsn, $user, $password);
// 组装语句并发送执行
$sql = "SELECT * FROM tushu";
$data = $dbh->query($sql);
// 从结果集对象中提取数据(数据格式直接接收为二维数组格式)
if($data)
$personlist = $data->fetchAll(PDO::FETCH_NUM);
$dbh = null;
} catch (PDOException $e) {
echo '连接失败: ' . $e->getMessage();
}
?>
<table>
<tr><th>序号</th><th>书名</th><th>简介</th><th>作者</th><th>出版社</th><th>编号</th><th>出版时间</th><th>价格</th><th>库存</th></tr>
<?php
if(isset($personlist) && is_array($personlist) && count($personlist)>0){
foreach($personlist as $person){
echo '<tr>';
foreach($person as $key=>$info){
if($key==2){
continue;
}
if($key==3) {
echo"<td width='300px'>$info</td>";
}else
echo "<td>$info</td>";
}
echo '</tr>';
}
}else{
echo '<tr><td colspan="9">没有查询结果</td></tr>';
}
?>
</table>
<style>
table{
border: 1px solid gray;
border-collapse: collapse;
width: 1300px;
}
table th, table td{
border: 1px solid gray;
}
</style>
</body>
</html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$dsn = 'mysql:dbname=test; host=127.0.0.1; port=3306; charset=utf8';
$user = 'root' ;
$password = '' ;
try {
// 创建对象(已经包含了连接了,同时也确定了数据库,还同时设置了字符集)
$dbh = new PDO($dsn, $user, $password);
// 组装语句并发送执行
$sql = "SELECT * FROM tushu";
$data = $dbh->query($sql);
// 从结果集对象中提取数据(数据格式直接接收为二维数组格式)
if($data)
$personlist = $data->fetchAll(PDO::FETCH_NUM);
$dbh = null;
} catch (PDOException $e) {
echo '连接失败: ' . $e->getMessage();
}
?>
<table>
<tr><th>序号</th><th>书名</th><th>简介</th><th>作者</th><th>出版社</th><th>编号</th><th>出版时间</th><th>价格</th><th>库存</th></tr>
<?php
if(isset($personlist) && is_array($personlist) && count($personlist)>0){
foreach($personlist as $person){
echo '<tr>';
foreach($person as $key=>$info){
if($key==2){
continue;
}
if($key==3) {
echo"<td width='300px'>$info</td>";
}else
echo "<td>$info</td>";
}
echo '</tr>';
}
}else{
echo '<tr><td colspan="9">没有查询结果</td></tr>';
}
?>
</table>
<style>
table{
border: 1px solid gray;
border-collapse: collapse;
width: 1300px;
}
table th, table td{
border: 1px solid gray;
}
</style>
</body>
</html>