myconfig25.php
定义链接数据库常量
<span style="font-size:24px;"><span style="font-size:24px;"><span style="font-size:24px;"><?php
//定义链接数据库常量
/**
* Created by PhpStorm.
* User: hao
* Date: 2015/9/3
* Time: 7:45
*/
//定义的常亮
//本地
define('MYSQL_HOST','localhost');
//用户名
define('MYSQL_ROOT','root');
//密码
define('MYSQL_PW','');</span></span></span>
myfunctions25.php
链接数据库
<span style="font-size:24px;"><span style="font-size:24px;"><span style="font-size:24px;"><?php
//链接数据库
/**
* Created by PhpStorm.
* User: hao
* Date: 2015/9/3
* Time: 7:50
*/
require_once 'myconfig25.php';
//链接服务器的方法:
function connectDb(){
// 链接服务器
$conn=mysql_connect(MYSQL_HOST,MYSQL_ROOT,MYSQL_PW);
if(!$conn){
die('can not conn db');
}
// 选择数据库
mysql_select_db('myapp');
return $conn;
}</span></span></span>
index25.php
从mysql数据库中查询到数据,最终写到html表格中,添加删除一列,点击跳转到删除界面
delete25.php<span style="font-size:24px;"><span style="font-size:24px;"><?php //删除用户数据 //从mysql数据库中查询到数据,最终写到html表格中,添加删除一列,点击跳转到删除界面 require_once 'myfunctions25.php'; ?> <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <table width='500px' border='1' style='border-collapse:collapse;text-align: left '> <tr> <th>id</th> <th>名字</th> <th>年龄</th> <th>删除</th> </tr> <?php //链接数据库 $conn = connectDb(); if ($conn) { echo '链接成功'; //选择数据库 mysql_select_db('myapp', $conn); //查询表中所有的数据 //改变sql语句,可以随意查询 $result = mysql_query("SELECT * FROM mytable"); //mytable表中有多少条数据 $mycount = mysql_num_rows($result); print_r($mycount); //循环拿出所有数据 for ($i = 0; $i < $mycount; $i++) { //把数据转化成数组 $result_arr = mysql_fetch_assoc($result); //打印出数组数据 print_r($result_arr); // 打印出来3条数据,这里我就写了1条,其他模式都一样 // Array // ( // [_id] =>; 1 // [_name] =>; zhang3 // [_age] =>; 12 //) $id = $result_arr['_id']; $name = $result_arr['_name']; $age = $result_arr['_age']; echo "<tr><td>$id</td><td>$name</td><td>$age</td><td><a href='delete25.php?id=$id'>删除</a></td></tr>"; } } else { echo '链接失败'; } ?> <table/> </body> </html> </span></span>
链接数据库,删除数据库中的某条数据,并跳转到查询界面
源码下载:<span style="font-size:24px;"><span style="font-size:24px;"><?php //链接数据库,删除数据库中的某条数据,并跳转到查询界面 /** * Created by PhpStorm. * User: hao * Date: 2015/9/3 * Time: 18:31 */ if(empty($_GET['id'])){ die("id is empty"); } require_once 'myfunctions25.php'; connectDb(); $id=intval($_GET['id']); mysql_query("DELETE FROM mytable WHERE _id = $id"); if(mysql_errno()){ die("huo qu id shi bai $id"); }else{ header("Location:index25.php"); } </span></span>
http://download.csdn.net/detail/zhaihaohao1/9079123