微信公众号开发之(16)连接数据库

连接数据库

  • 新浪云mySQL
  • phpMyAdmin管理
  • 创建表、字段
  • 连接数据库
  • select
  • updata

我用到的数据库是:新浪云共享型数据库

微信公众号开发之(16)连接数据库_第1张图片

如何在新浪云上连接数据库:
推荐一个人寻觅beyond写的博客:https://www.cnblogs.com/-beyond/p/6739737.html

我连接数据库代码如下:


<head>
    <meta charset="utf-8" />
head>
<body>
    连接数据库

     $dbname = "数据库名";
     $host = "主库域名";
     $port = "端口";
     $user = "用户名";
     $pwd = "密码";
     mysql_connect($host,$user,$pwd) or die("can ' t connect to MySQL");
     mysql_select_db($dbname)
?>
body>

微信公众号开发之(16)连接数据库_第2张图片
核心代码如下:

//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 ))
                {
                    //用 户 名 :  $user
                    //密  码 :  $pwd
                    //主库域名 :  $host
                    //从库域名 :  SAE_MYSQL_HOST_S
                    //端  口 :  $port
                    //数据库名 :  $dbname

                    $dbname = "app_dq095";
                    $host = "w.rdc.sae.sina.com.cn";
                    $port = "3306";
                    $user = "用户名";
                    $pwd = "密码";

                    /*接着调用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_select_db来切换到其它数据库了!!!*/
                    /* 需要再连接其它数据库,请再使用mysql_connect+mysql_select_db启动另一个连接*/

                    /**
                     * 接下来就可以使用其它标准php mysql函数操作进行数据库操作
                     */
                     mysql_query("set names utf-8",$db); 
                     $sql="SELECT * FROM  `choujiang` WHERE 1 LIMIT 0 , 30";
                     $query=mysql_query($sql);
                     $rs=mysql_fetch_array($query);
                     $b= $rs['num'];
                    $msgType = "text";
                    $contentStr = "你是第{$b}个睡觉的!";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);                  
                    echo $resultStr;
                    $b++;
                    $sql="UPDATE `choujiang` SET `num`={$b} WHERE 1";
                    mysql_query($sql);
                    mysql_close($link);
                 }else{
                        echo "Input something...";
                 }

index.php代码如下:


/**
  * wechat php test
  */

//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 ))
                {
                    //用 户 名 :  $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_select_db来切换到其它数据库了!!!*/
                    /* 需要再连接其它数据库,请再使用mysql_connect+mysql_select_db启动另一个连接*/

                    /**
                     * 接下来就可以使用其它标准php mysql函数操作进行数据库操作
                     */
                     mysql_query("set names utf-8",$db); 
                     $sql="SELECT * FROM  `choujiang` WHERE 1 LIMIT 0 , 30";
                     $query=mysql_query($sql);
                     $rs=mysql_fetch_array($query);
                     $b= $rs['num'];
                    $msgType = "text";
                    $contentStr = "你是第{$b}个睡觉的!";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);                  
                    echo $resultStr;
                    $b++;
                    $sql="UPDATE `choujiang` SET `num`={$b} WHERE 1";
                    mysql_query($sql);
                    mysql_close($link);
                 }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;
        }
    }
}

?>

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