测试php连接mysql的实例实现

1.在linux上搭建好lnmp架构后,先测试一下php是否可以连接上mysql.

测试代码:test.php

<?php 
$link=mysql_connect("172.25.90.14","root","redhat"); 
if(!$link) echo "FAILD!连接错误,用户名密码不对"; 
else echo "OK!可以连接"; 

?>

2.接下来利用php就可以对mysql进行增删改查了

a.  config.php 首先可创建一个保存常量的config.php

<?php  
define('MYSQL_HOST','172.25.90.14');  

define('MYSQL_USER','root'); 

define('MYSQL_PW','redhat');

b.  functions.php 再创建一个保存函数的文件functions.php:

<?php  
require_once 'config.php';  
function connnetDb(){  
    //连接mysql数据库  
    $conn=mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_PW);  
    //排除连接数据库异常错误  
    if(!$conn){  
        die('can not connect db');  
    }  
    //在mysql中选中myapp数据库  
    mysql_select_db("wzw");  
    return $conn;  

}

c.  allusers.php  查询数据库

<?php  
    require_once 'functions.php';  
?>  
<!DOCTYPE html>  
<html>  
<head>  
    <meta charset="UTF-8">  
    <title>所有用户</title>  
    <style>  
        table{  
            border-collapse: collapse;  
        }  
        th,td{  
            border:1px solid #ccccff;  
            padding: 5px;  
        }  
        td{  
            text-align: center;  
        }  
    </style>  
</head>  
<body>  
<a href="adduser.html">添加用户</a>  
<table>  
    <tr><th>id</th><th>名字</th><th>年龄</th><th>修改/删除</th></tr>  
<?php  
//连接数据库  
connnetDb();  
//查询数据表中的所有数据,并按照id降序排列  
$result=mysql_query("SELECT * FROM users ORDER BY id DESC");  
//获取数据表的数据条数  
$dataCount=mysql_num_rows($result);  
//echo $dataCount;  
//打印输出所有数据  
 
 
for($i=0;$i<$dataCount;$i++){  
    $result_arr=mysql_fetch_assoc($result);  
    $id=$result_arr['id'];  
    $name=$result_arr['name'];  
    $age=$result_arr['age'];  
    //print_r($result_arr);  
    echo "<tr><td>$id</td><td>$name</td><td>$age</td><td><a href='edituser.php?id=$id'>修改</a> <a href='deleteuser.php?id=$id'>删除</a></td></tr>";  
}  
?>  
</table>  
</body>  

</html>

d.  adduser.html  新建adduser.html页面,提供用户输入表单

<!DOCTYPE html>  
<html>  
<head lang="en">  
    <meta charset="UTF-8">  
    <title>添加用户</title>  
</head>  
<body>  
<form action="adduser.php" method="post">  
    <label>用户ID:</label><input type="text" name="id">
    <label>用户名:</label><input type="text" name="name">  
    <label>年龄:</label><input type="text" name="age">  
    <input type="submit" value="提交">  
</form>  
</body>  

</html>

e.  adduser.php  创建处理用户表单数据的服务端文件adduser.php,并将添加的数据返回到列表页面

<?php  
require_once 'functions.php';  
//首先进行非空排错
if(!isset($_POST['id'])){
    die('id is not define');
}  
if(!isset($_POST['name'])){  
    die('name is not define');  
}  
if(!isset($_POST['age'])){  
    die('age is not define');  
}  
$id=$_POST['id'];
$name=$_POST['name'];  
$age=$_POST['age'];  
if(empty($id)){
    die('id is empty');
}
if(empty($name)){  
    die('name is empty');  
}  
if(empty($age)){  
    die('age is empty');  
}  
//连接数据库  
connnetDb();  
 
 
//执行类型转换,防止SQL注入  
$age=intval($age);  
//插入数据  
mysql_query("INSERT INTO users(id,name,age) VALUES ('$id','$name','$age')");  
//返回列表页面  
if(mysql_error()){  
    echo mysql_error();  
}else{  
    header("Location:allusers.php");  

}

f.  edituser.php  创建edituser.php,获取需要修改的数据并呈现成表单,供用户修改数据

<?php  
require_once 'functions.php';?>  
<!DOCTYPE html>  
<html>  
<head>  
    <meta charset="UTF-8">  
    <title>修改用户数据</title>  
</head>  
<body>  
<?php  
    if(!empty($_GET['id'])){  
        //连接mysql数据库  
        connnetDb();  
 
 
        //查找id  
        $id=intval($_GET['id']);  
        $result=mysql_query("SELECT * FROM users WHERE id=$id");  
        if(mysql_error()){  
            die('can not connect db');  
        }  
        //获取结果数组  
        $result_arr=mysql_fetch_assoc($result);  
    }else{  
        die('id not define');  
    }  
?>  
<form action="edituser_server.php" method="post">  
    <label>用户ID:</label><input type="text" name="id" value="<?php echo $result_arr['id']?>">  
    <label>用户名:</label><input type="text" name="name" value="<?php echo $result_arr['name']?>">  
    <label>用户年龄:</label><input type="text" name="age" value="<?php echo $result_arr['age']?>">  
    <input type="submit" value="提交修改">  
</form>  
</body>  

</html>

g.  edituser_server.php  提交给服务端edituser_server.php处理:

<?php  
    require_once 'functions.php';  
    if(empty($_POST['id'])){  
        die('id is empty');  
    }  
    if(empty($_POST['name'])){  
        die('name is empty');  
    }  
    if(empty($_POST['age'])){  
        die('age is empty');  
    }  
      
      
    $id=intval($_POST['id']);  
    $name=$_POST['name'];  
    $age=intval($_POST['age']);  
      
      
    //连接数据库  
    connnetDb();  
      
      
    //修改指定数据  
    mysql_query("UPDATE users SET name='$name',age=$age WHERE id=$id");  
      
      
    //排错并返回  
    if(mysql_error()){  
        echo mysql_error();  
    }else{  
        header("Location:allusers.php");  

    }

h.  deleteuser.php  执行删除操作后返回主页面

<?php  
    require_once 'functions.php';  
      
    //排空错误  
    if(empty($_GET['id'])){  
        die('id is empty');  
    }  
    //连接数据库  
    connnetDb();  
      
    $id=intval($_GET['id']);  
      
    //删除指定数据  
    mysql_query("DELETE FROM users WHERE id=$id");  
    //排错并返回页面  
    if(mysql_error()){  
        echo mysql_error();  
    }else{  
        header("Location:allusers.php");  

    }




猜你喜欢

转载自blog.csdn.net/weixin_40555670/article/details/80583534
今日推荐