微信公众号开发9连接数据库

还有解决不了的欢迎留言,也可以访问我的微博,知无不言,菜鸟一枚,感谢观看    http://weibo.com/rosekissyou

1 这里首先在服务器新建数据库  wechart_official  采用默认的utf8编码
然后建立测试的数据表

微信公众号开发9连接数据库_第1张图片

这里新建了表   weae_user  新建了一条数据

user_id (INT10) user_name(varchar60)  createtime (datetime)

2 修改代码的数据库配置文件   Common/Conf/config.php

'DB_TYPE'=>'mysql',// 数据库类型

'DB_HOST'=>'127.0.0.1',// 服务器地址

'DB_NAME'=>'wechart_official',// 数据库名

'DB_USER'=>'root',// 用户名

'DB_PWD'=>'自己的密码',// 密码

'DB_PORT'=>'3306',// 端口

'DB_PREFIX'=>'weae_',// 数据库表前缀

'DB_PARAMS'=>array(),// 数据库连接参数

'DB_DEBUG'=>TRUE,// 数据库调试模式 开启后可以记录SQL日志

'DB_FIELDS_CACHE'=>true,// 启用字段缓存 , 在调试模式下字段缓存和数据库缓存都不起作用

'DB_CHARSET'=>'utf8',// 数据库编码默认采用utf8

'DB_DEPLOY_TYPE'=>0,// 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)

'DB_RW_SEPARATE'=>false,// 数据库读写是否分离 主从式有效

'DB_MASTER_NUM'=>1,// 读写分离后 主服务器数量

'DB_SLAVE_NO'=>'',// 指定从服务器序号

/*模板引擎更换

// 布局设置  可以更改为smarty引擎

'TMPL_ENGINE_TYPE'      =>  'Think',    // 默认模板引擎 以下设置仅对使用Think模板引擎有效

*/

// 布局设置

'TMPL_ENGINE_TYPE'=>'Smarty',// 默认模板引擎 以下设置仅对使用Think模板引擎有效

上面我直接就把模板引擎改成了smarty

3 添加model模块,在根目录下面新建Model ,然后分别新建IndexModel.class.php MenuModel.class.php UserModel.class.php

这里我主要使用IndexModel做测试,所以先在这里添加命名空间和继承父类Model

namespaceModel;

useThink\Model;

classIndexModelextendsModel{

}

接着我们在IndexController里面添加调用

user Model/IndexModel;

然后就可以开始测试了;

4 接下来开始连接服务器测试,先在IndexController控制器里面添加一个方法测试服务器

public functionuser_sql(){

$sql_user=new\Model\UserModel();

$select_user=$sql_user->select();

var_dump($select_user);

$this->display('index');

}

返回如下内容


微信公众号开发9连接数据库_第2张图片

这里也就表示数据库连接是正常的,下面开始在微信公众号里面使用数据库连接;

微信公众号开发9连接数据库_第3张图片

这里添加当用户输入 '测试数据库连接' 的时候调用数据库,就会显示博客里面的标题


微信公众号开发9连接数据库_第4张图片


实现效果如上图, 源码在下面直接给出,欢迎吐槽,也欢迎交流,一起共同进步,谢谢


namespaceAdmin\Controller;

useModel\ArticlesModel;

useModel\IndexModel;

useThink\Controller;

classIndexControllerextendsController {

//进行微信测试,跳过验证

public functionindex(){

//    $this->display();

self::responseMsg();

}

public functionvalid()

{

$echoStr=$_GET["echostr"];

//valid signature , option

if($this->checkSignature()){

echo$echoStr;

exit;

}

}

//消息回复

public functionresponseMsg()

{

//get post data, May be due to the different environments

$postStr=$GLOBALS["HTTP_RAW_POST_DATA"];

//extract post data

if(!empty($postStr)){

$postObj=simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);

$fromUsername=$postObj->FromUserName;

$toUsername=$postObj->ToUserName;

$keyword=trim($postObj->Content);

$input_type=$postObj->MsgType;// 分类获取不同的输入信息

$loc_x=$postObj->Location_X;

$loc_y=$postObj->Location_Y;

$time=time();

$textTpl="

%s

0

";

// 1 获取关注后的动作

$ev=$postObj->Event;

if($ev=="subscribe")

{

$msgType="text";

$contentStr="感谢你的关注百姓堂公众号,只为给您更好更健康的身体而存在!";

$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);

echo$resultStr;

}

// 2 分类解析不同类型的输入信息

// 2.1 文本消息

if($input_type=="image") {

$msgType="text";

$contentStr="掐指一算,你今天缺我";

$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);

echo$resultStr;

}

//1  发送定位返回附件的酒店

if($input_type=="location")

{

$textTpl="

%s

4

1

";

$url="http://api.map.baidu.com/telematics/v2/local?location={$loc_y},{$loc_x}&keyWord=酒店&number=3&ak=1a3cde429f38434f1811a75e1a90310c";

$fa=file_get_contents($url);

$f=simplexml_load_string($fa);

$d1=$f->poiList->point[0]->name;

$d2=$f->poiList->point[1]->name;

$d3=$f->poiList->point[2]->name;

$w1=$f->poiList->point[0]->address;

$w2=$f->poiList->point[1]->address;

$w3=$f->poiList->point[2]->address;

$p1=$f->poiList->point[0]->telephone;

$p2=$f->poiList->point[1]->telephone;

$p3=$f->poiList->point[2]->telephone;

$q1=$f->poiList->point[0]->distance;

$q2=$f->poiList->point[1]->distance;

$q3=$f->poiList->point[2]->distance;

$m1="{$d1}地址{$w1}电话{$p1}距离{$q1}米";

$m2="{$d2}地址{$w2}电话{$p2}距离{$q2}米";

$m3="{$d3}地址{$w3}电话{$p3}距离{$q3}米";

$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$m1,$m2,$m3);

echo$resultStr;

}

if(!empty($keyword))

{

$msgType="text";

switch($keyword)

{

case'测试数据库连接':

//测试调用数据库

$sql_articles=new\Model\ArticlesModel();

$articles=$sql_articles->select();

$contentStr='';

foreach($articlesas$article){

echo$article['title'];

$contentStr.=$article['title'].'  ';

}

//  $contentStr = "请发送定位信息给我";

$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);

echo$resultStr;

break;

case'1';

$textTpl="

%s

0

";

// $contentStr = "请搜索小程序 百姓堂 允许获取定位来使用该功能";

$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time);

echo$resultStr;

break;

case'2':

$contentStr="您有什么健康问题呢?请发送关键词,或者关注公众号: hello kitty 获取更多相关健康问题";

$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);

echo$resultStr;

break;

case'3':

$contentStr="请发送定位信息给我";

$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);

echo$resultStr;

break;

case'5':

$contentStr="发送你的靓照给我,立刻给你算算鸿运,道破吉凶";

$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);

echo$resultStr;

break;

case"6";

$textTpl="

%s

1

1

";

$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time);

echo$resultStr;

break;

case'7':

$textTpl="

%s

1

1

";

$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time);

echo$resultStr;

break;

default:

$contentStr=<<< MESG

欢迎访问百年臻阳方,本公众号由广东泓然堂医药有限公司提供,为您的健康提供全方位保障,点击关注有惊喜.

1  歌曲 张碧晨-年轮

2  咨询健康问题

3  发送定位寻找附件的酒店 指导您来到本店

5  发送相片给我,半仙帮你看相

6  易企秀

7  贺卡制作

MESG;

$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);

echo$resultStr;

break;

}

//                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);

//                echo $resultStr;

}else{

echo"Input something...";

}

}else{

echo"";

exit;

}

}

//  微信验证

private functioncheckSignature()

{

$signature=$_GET["signature"];

$timestamp=$_GET["timestamp"];

$nonce=$_GET["nonce"];

$token=TOKEN;

$tmpArr=array($token,$timestamp,$nonce);

sort($tmpArr);

$tmpStr=implode($tmpArr);

$tmpStr=sha1($tmpStr);

if($tmpStr==$signature){

return true;

}else{

return false;

}

}

public functionsql_test(){

$sql_articles=new\Model\ArticlesModel();

$articles=$sql_articles->select();

foreach($articlesas$article){

echo$article['title'];

}

$this->assign('articles',$articles);

//  var_dump($articles);

$this->display('index');

}

public functionuser_sql(){

$sql_user=new\Model\UserModel();

$select_user=$sql_user->select();

var_dump($select_user);

$this->display('index');

}

}

还有解决不了的欢迎留言,也可以访问我的微博,知无不言,菜鸟一枚,感谢观看    http://weibo.com/rosekissyou

你可能感兴趣的:(微信公众号开发9连接数据库)