以下是一个连接数据库,打印数据表,并能够实时删除数据的php代码实例,供大家学习和使用。主要包括首页index.php,连接数据库操作conn.php,删除操作delete.php。效果图如下:
代码如下:
数据库在这篇文章末尾:http://liuyanzhao.com/1894.html
代码如下:
index.php
- <?php
- //包含conn.php文件
- include("conn.php");
- //执行查询的SQL语句
- $sql = "SELECT * FROM 007_news ORDER BY id asc";
- $result = mysql_query($sql); //向MySQL服务器发出SQL请求
- //取出记录总数
- $records = mysql_num_rows($result);
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html>
- <head>
- <title>网站首页</title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <script type="text/javascript">
- function confirmDel(id)
- {
- if(window.confirm("你确认要删除吗?"))
- {
- //跳转到delete.php文件
- location.href = "delete.php?id="+id;
- }
- }
- </script>
- </head>
- <body>
- <table width="1000" border="1" bordercolor="#eee" rules="all" align="center" cellpadding="5">
- <tr>
- <th>编号</th>
- <th>新闻标题</th>
- <th>作者</th>
- <th>来源</th>
- <th>点击率</th>
- <th>发布时间</th>
- <th>操作选项</th>
- </tr>
- <?php
- while($row=mysql_fetch_row($result))
- {
- ?>
- <tr align="center">
- <td><?php echo $row[0] ?></td>
- <td align="left"><a href="javascript:void(0)"><?php echo $row[2] ?></a></td>
- <td><?php echo $row[3] ?></td>
- <td><?php echo $row[4] ?></td>
- <td><?php echo $row[9] ?></td>
- <td><?php echo date("Y-m-d H:i",$row[10])?></td>
- <td>
- <a href="javascript:void(0)" onClick="confirmDel(<?php echo $row[0] ?>)">删除</a>
- </td>
- </tr>
- <?php
- }
- ?>
- </table>
- </body>
- </html>
conn.php
- <?php
- //用于声明或设置网页中的字符集的
- header("content-type:text/html;charset=utf-8");
- //(1)数据库配置信息
- $db_host = "localhost";
- $db_user = "root";
- $db_pwd = "";
- $db_name = "saixinjituan"; //自定义数据库名称
- //(2)连接MySQL服务器
- $link = @mysql_connect($db_host,$db_user,$db_pwd);
- if(!$link)
- {
- echo "<li>PHP连接MySQL服务器出错啦!</li>";
- exit();
- }
- //(3)选择当前数据库
- if(!mysql_select_db($db_name))
- {
- echo "<li>选择数据库<font color='red'>{$db_name}</font>失败!</li>";
- exit();
- }
- //(4)设置MySQL返回数据的字符集
- mysql_query("set names utf8");
- ?>
delete.php
- <?php
- //包含conn.php文件
- require("conn.php");
- //获取地址的id参数,并构建要删除的SQL语句
- $id = $_GET["id"];
- $sql = "DELETE FROM 007_news WHERE id=$id";
- //执行查询的SQL语句
- if(mysql_query($sql))
- {
- //跳转到首页index.php
- header("location:index.php");
- }
- ?>
数据库在这篇文章末尾:http://liuyanzhao.com/1894.html
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏