php redis实例
test.php
<?php
$redis = new Redis();
$redis->connect(‘127.0.0.1‘,6379);
?>
===========================
add.php
<form method=‘POST‘ action=‘reg.php‘>
用户名:<input type=‘text‘ name=‘username‘/>
密码:<input type=‘password‘ name=‘pwd‘/>
年龄:<input type=‘text‘ name=‘age‘/>
<input type=‘submit‘ value=‘提交‘>
<input type=‘reset‘ value=‘重新填写‘>
</form>
==============================
reg.php
<?php
require(‘test.php‘);
$username = $_POST[‘username‘];
$pwd = $_POST[‘pwd‘];
$age = $_POST[‘age‘];
$uid = $redis->incr(‘userId‘);
$redis->hmset("user:".$uid,array(‘uid‘=>$uid, ‘username‘=>$username, ‘pwd‘=>$pwd, "age"=>$age));
//print_r($redis->keys("*"))
header("location:list.php");
?>
=============
list.php
<?php
require(‘test.php‘);
$count = $redis->get(‘userId‘);
//echo $count;
for ($i=1; $i<=$count; $i++) {
// print_r($redis->hGetAll("user:".$i));
$data[] = $redis->hGetAll("user:".$i);
}
$data = array_filter($data);//过滤掉空数组(删除造成的)
?>
<a href=‘add.php‘>注册</a>
<table border=‘1‘>
<tr>
<th>姓名</th>
<th>密码</th>
<th>年龄</th>
<th>操作</th>
</tr>
<?php
foreach ($data as $v) {
?>
<tr>
<td><?php echo $v[‘username‘];?></td>
<td><?php echo $v[‘pwd‘];?></td>
<td><?php echo $v[‘age‘];?></td>
<td><a href="del.php?id=<?php echo $v[‘uid‘]; ?>">删除</a><a href="modify.php?id=<?php echo $v[‘uid‘]; ?>">修改</a></td>
</tr>
<?php }
?>
</table>
=========================
del.php
<?php
require(‘test.php‘);
$uid = $_REQUEST[‘id‘];
//$uid=‘10000‘;
$num = $redis->del("user:".$uid);
//echo $num;exit();
if ($num == 0) {
echo "删除失败";
} else {
header("location:list.php");
}
?>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。