|
- <?php
- // 数据库的增加、查找,更新和删除-----------------------------------------------
- // 正则表达式/^报名/ 只匹配报名开头的关键字语句
- // preg_match_all('/^报名 (.+)/',"报名 张三",$matches); $matches[1][0]返回的是张三
- // 新建模块:合局设置,新建模块,模块类型(其它),需要嵌入的规则,生成,安装
- // 模块路径:根/public_html/source/modules
- //查找
- $content = $this->message['content'];
- $openid = $this->message['from'];
- $sql = "select * from ".tablename('register')." where openid = :openid";
- $array = array(':openid'=>$openid);
- //如果注册成功
- $msg = '';
- if($res=pdo_fetch($sql,$array)){
- $msg = "你已经注册,\n注册名是:$res['name']"; //双引号里面才有用\n换行
- }else{
- //增加
- preg_match_all('/^报名 (.+)/',$content,$matches);
- $res = pdo_insert('regist',array('openid'=>$openid,'name'=>$matches[1][0]));
- if($res)
- {
- $msg = "注册成功!";
- }else{
- $msg = "注册失败!";
- }
- }
- return $this ->respText['$msg'];
- // 删除 pdo_delete(‘表名’,条件关联数组$arr,and/or 默认是and);
- $openid = $this->message['from'];
- $arr = arry('openid'=>$openid); //不加条件默认是删除所有数据
- $res = pdo_delete('tablename',$arr); //返回1 影响一条
- return $this->respText($res); //respNews 图文
- //更新数据
- $whereArr = arry('openid'=>$openid);
- $dataArr = array('name'=>'李四');
- //对应参数 表名,更新的数据,更新的条件
- $res = pdo_update('register',$dataArr,$whereArr);//对应参数 表名,更新的数据,更新的条件
- return $this->respText($res);
- //replace 删除更新 根据openid 的唯一性,如果二次插入同一个openid一样的数据 将先删除后插入,影响两行记录
- $sql = "replace into ".tablename('register')." ('openid','name') values ('".$openid."','王五')";
- $res = pdo_query($sql); //返回1或者2 删除影响1 插入影响1
- return $this->respText($res); //
- //通讯录模块的开发-----------------------------------------------------
- //数据库表结构
- create table if not exists 'ims_contacts'(
- 'id' int(11) not null auto_increment,
- 'openid' varchar(40) not null comment '微信openid',
- 'name' varchar(40) not null comment '名字',
- 'phone' varchar(40) not null comment '手机号',
- primary key ('id')
- )engine=myisam default charset=utf8;
复制代码
|
|