数据操作类使用 ——读取列表
原理也差不多,除了执行外,还要再读取数据。
简单再归纳一下:看下三行加亮的代码就可以知道怎么使用了。
<?php
require('common.php');
$result = $query->query("SELECT * FROM gb_content order by id desc");//查询数据
while ($row = $query->fetch_array($result)) {// 取一条数据
?>
<table width="700" border="0" cellspacing="0" cellpadding="0" class="tb">
<tr>
<td class="bg"><b>[
<?php echo htmlentities($row['username']) ?>]</b> 发表于:<?php echo htmlentities($row['insert_time']) ?></td>
</tr>
<tr>
<td><?php echo preg_replace('/(http[s]?:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"\s])*)/i','<a target="_blank" href="$1">$1</a>',htmlentities($row['content'])) ?>
代码如下:
复制内容到剪贴板
代码:
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>所有留言</title>
<link rel="stylesheet" type="text/css" href="style.css" media="all" />
</head>
<body>
<?php
if ($_SESSION['user_id']) {
echo 'Welcome:'.$_SESSION['username'];
echo '<a href="logout.php">退出</a>';
} else {
?>
<a href="login.php">登录</a>
<a href="reg.php">注册</a>
<?php
}
?>
<a href="add.php">发表留言</a>
<a target="_blank" href="gblist.php">更多留言</a>
<?php
require('common.php');
$result = $query->query("SELECT * FROM gb_content order by id desc");//查询数据
while ($row = $query->fetch_array($result)) {// 取一条数据
?>
<table width="700" border="0" cellspacing="0" cellpadding="0" class="tb">
<tr>
<td class="bg"><b>[<?php echo htmlentities($row['username']) ?>]</b> 发表于:<?php echo htmlentities($row['insert_time']) ?></td>
</tr>
<tr>
<td><?php echo preg_replace('/(http[s]?:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"\s])*)/i','<a target="_blank" href="$1">$1</a>',htmlentities($row['content'])) ?>
<br />
<?php
if ($row['user_file']) {
echo '附件:<a target="_blank" href="uploads/'.$row['user_file'].'">'.$row['user_file'].'</a>';
}
?>
</td>
</tr>
<tr>
<td align="right"><a href="edit.php?id=<?php echo $row['id'] ?>">修改</a> <a href="delete.php?id=<?php echo $row['id'] ?>">删除</a></td>
</tr>
</table>
<?php
}
$query->free_result($result);
$query->close();
?>
</body>
</html>