微信公众号开发之(19)导入数据库

导入数据库

导入文件格式:*.csv
下载:shenfen.csv

数据库学习:

自定义方法search

 - function search($a){
 - ····
 -  return $b
 - }
 - $rs = search($keyword);

微信公众号开发之(19)导入数据库_第1张图片微信公众号开发之(19)导入数据库_第2张图片
核心代码如下:

function chaxun($a){
        //用 户 名 :  $user
        //密  码 :  $pwd
        //主库域名 :  $host
        //从库域名 :  SAE_MYSQL_HOST_S
        //端  口 :  $port
        //数据库名 :  $dbname

        $dbname = "app_dq095";
        $host = "w.rdc.sae.sina.com.cn";
        $port = "3306";
        $user = "4k514n103z";
        $pwd = "2402314li2j1i5im1xy2xizj5y332w2x41k2z203";

        /*接着调用mysql_connect()连接服务器*/
        // 连主库
        $db = mysql_connect($host,$user,$pwd);

        if(!$db){
            die("Connect Server Failed: " . mysql_error($db));
        }

        /*连接成功后立即调用mysql_select_db()选中需要连接的数据库*/
        if (!mysql_select_db($dbname)) {
            die("Select Database Failed: " . mysql_error($db));
        }
        mysql_query("set names utf-8",$db); 

        /*至此连接已完全建立,就可对当前数据库进行相应的操作了*/      
        /**
          * 接下来就可以使用其它标准php mysql函数操作进行数据库操作
        */

        mysql_query("set names utf-8",$link); 

        $sql="SELECT `DQ` FROM `shen` WHERE `BM`='" . iconv("UTF-8","GBK",$a) . "'";
        $query=mysql_query($sql);
        $rs=mysql_fetch_array($query);
        $c= $rs['DQ'];
        $b=iconv("GBK","UTF-8",$c);
        if (empty($b)){
            $c="对不起,暂无此数据";
        }
        return $c;
        mysql_close($db);
}

//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();

调用方法如下:



 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  $fromUsername = $postObj->FromUserName;
  $toUsername = $postObj->ToUserName;
  $keyword = trim($postObj->Content);
  $time = time();
  $textTpl = "<xml>
              <ToUserName>ToUserName>
              <FromUserName>FromUserName>
              <CreateTime>%sCreateTime>
              <MsgType>MsgType>
              <Content>Content>
              <FuncFlag>0FuncFlag>
              xml>";   

 if(!empty($keyword ))
{
$msgType = "text";
$contentStr= chaxun($keyword);

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

 }else{
    echo "Input something...";
 }

数据库代码如下:

-- phpMyAdmin SQL Dump
-- version 3.3.8.1
-- http://www.phpmyadmin.net
--
-- 主机: w.rdc.sae.sina.com.cn:3307
-- 生成日期: 2018 年 08 月 24 日 00:32
-- 服务器版本: 5.6.23
-- PHP 版本: 5.3.3

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- 数据库: `app_dq095`
--

-- --------------------------------------------------------

--
-- 表的结构 `shen`
--

CREATE TABLE IF NOT EXISTS `shen` (
  `id` int(8) NOT NULL AUTO_INCREMENT,
  `BM` int(8) NOT NULL,
  `DQ` varchar(76) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ;

--
-- 转存表中的数据 `shen`
--

INSERT INTO `shen` (`id`, `BM`, `DQ`) VALUES
(1, 110000, '北京市'),
(2, 110100, '北京市市辖区'),
(3, 110101, '北京市东城区'),
(4, 110102, '北京市西城区'),
(5, 110103, '北京市崇文区'),
(6, 110104, '北京市宣武区'),
(7, 110105, '北京市朝阳区'),
(8, 110106, '北京市丰台区'),
(9, 110107, '北京市石景山区'),
(10, 110108, '北京市海淀区');

index.php整体代码如下:


/**
  * wechat php test
  */

    function chaxun($a){
        //用 户 名 :  $user
        //密  码 :  $pwd
        //主库域名 :  $host
        //从库域名 :  SAE_MYSQL_HOST_S
        //端  口 :  $port
        //数据库名 :  $dbname

        $dbname = "app_dq095";
        $host = "w.rdc.sae.sina.com.cn";
        $port = "3306";
        $user = "4k514n103z";
        $pwd = "2402314li2j1i5im1xy2xizj5y332w2x41k2z203";

        /*接着调用mysql_connect()连接服务器*/
        // 连主库
        $db = mysql_connect($host,$user,$pwd);

        if(!$db){
            die("Connect Server Failed: " . mysql_error($db));
        }

        /*连接成功后立即调用mysql_select_db()选中需要连接的数据库*/
        if (!mysql_select_db($dbname)) {
            die("Select Database Failed: " . mysql_error($db));
        }
        mysql_query("set names utf-8",$db); 

        /*至此连接已完全建立,就可对当前数据库进行相应的操作了*/      
        /**
          * 接下来就可以使用其它标准php mysql函数操作进行数据库操作
        */

        mysql_query("set names utf-8",$link); 

        $sql="SELECT `DQ` FROM `shen` WHERE `BM`='" . iconv("UTF-8","GBK",$a) . "'";
        $query=mysql_query($sql);
        $rs=mysql_fetch_array($query);
        $c= $rs['DQ'];
        $b=iconv("GBK","UTF-8",$c);
        if (empty($b)){
            $c="对不起,暂无此数据";
        }
        return $c;
        mysql_close($db);
}

//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();

class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }

    public function responseMsg()
    {
        //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);
                $time = time();
                $textTpl = "
                            
                            
                            %s
                            
                            
                            0
                            ";   

               if(!empty($keyword ))
               {
                    $msgType = "text";
                    $contentStr= chaxun($keyword);

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

               }else{
                    echo "Input something...";
               }


        }else {
            echo "";
            exit;
        }
    }

    private function checkSignature()
    {
        $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;
        }
    }
}

?>

你可能感兴趣的:(微信公众号开发)