微信公众号的数据库操作(mysql的php封装类)

joyshare.mysql.php

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

<?php
//引入数据库函数文件
require_once "MySQLi_BAE.class.php";

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

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

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

public function __construct()
{
$this->mysqli_BAE=new MySQLi_BAE();
}

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);
$RX_TYPE = trim($postObj->MsgType);

switch($RX_TYPE)
{
case "text":
$resultStr = $this->handleText($postObj);
break;
case "event":
$resultStr = $this->handleEvent($postObj);
break;
default:
$resultStr = "Unknow msg type: ".$RX_TYPE;
break;
}
echo $resultStr;
}else {
echo "";
exit;
}
}

public function handleText($postObj)
{
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";

if(!empty( $keyword ))
{
$msgType = "text";
$keywords = explode("+",$keyword);
//获取当前时间
$nowtime=date("Y-m-d G:i:s");

//调试用
//echo $this->responseText($postObj,$keyword);
//exit();

//判断是否已经绑定
$select_sql="SELECT id from test_mysql WHERE from_user='$fromUsername'";
$res=$this->mysqli_BAE->execute_dql($select_sql);
$rows=$res->fetch_array(MYSQLI_ASSOC);
if($rows[id] <> ''){
$user_flag='y';
}

if(trim($keywords[0] == '绑定')){
if($user_flag <> 'y'){
$insert_sql="INSERT INTO test_mysql(from_user, account, password, update_time) VALUES('$fromUsername','$keywords[1]','$keywords[2]','$nowtime')";
$res = $this->mysqli_BAE->execute_dml($insert_sql);
if($res == 1){
$contentStr = "绑定成功";
}elseif($res == 0){
$contentStr = "绑定失败";
}
}else{
$contentStr = "该账户已绑定";
}
}elseif(trim($keywords[0] == '查询')){
$select_sql="SELECT * FROM test_mysql WHERE from_user='$fromUsername'";
$select_res=$this->mysqli_BAE->execute_dql($select_sql);
$rows=$select_res->fetch_array(MYSQLI_ASSOC);
if($rows[id] <> ''){
$contentStr="账户:$rows[account]\n"."密码:$rows[password]\n"."From_user:$rows[from_user]\n"."更新时间:$rows[update_time]";
}else{
$contentStr="您还未绑定账户,查询不到相关信息,请先绑定,谢谢!";
}
}elseif(trim($keywords[0] == "修改")){
$old_password=$keywords[1];
$new_password=$keywords[2];
$select_password_sql="SELECT * FROM test_mysql WHERE from_user='$fromUsername'";
$select_res=$this->mysqli_BAE->execute_dql($select_password_sql);
$rows=$select_res->fetch_array(MYSQLI_ASSOC);
if($old_password == $rows[password]){
$update_sql="UPDATE test_mysql SET password='$new_password' WHERE from_user='$fromUsername'";
$res = $this->mysqli_BAE->execute_dml($update_sql);
if($res == 1){
$contentStr = "修改成功";
}elseif($res == 0){
$contentStr = "修改失败";
}
}else{
$contentStr = "原密码有误,请确认后重试";
}
}elseif(trim($keywords[0] == "删除")){
$delete_sql="DELETE FROM test_mysql WHERE from_user='$fromUsername'";
$res = $this->mysqli_BAE->execute_dml($delete_sql);
if($res == 1){
$contentStr = "删除成功";
}elseif($res == 0){
$contentStr = "删除失败";
}elseif($res == 2){
$contentStr = "账户未绑定,没有可删除对象";
}
}else{
$contentStr = "感谢您关注【视觉湄洲】"."\n"."微信号:mzdlynet"."\n"."使用以下方法测试数据库的使用\n"."1. 绑定+账户+密码\n"."2. 查询\n"."3. 修改+旧密码+新密码\n"."4. 删除";
}

$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo "Input something...";
}
}

public function handleEvent($object)
{
$contentStr = "";
switch ($object->Event)
{
case "subscribe":
$contentStr = "感谢您关注【视觉湄洲】"."\n"."微信号:mzdlynet"."\n"."使用以下方法测试数据库的使用\n"."1. 绑定+账户+密码\n"."2. 查询\n"."3. 修改+旧密码+新密码\n"."4. 删除";
break;
default :
$contentStr = "Unknow Event: ".$object->Event;
break;
}
$resultStr = $this->responseText($object, $contentStr);
return $resultStr;
}

public function responseText($object, $content, $flag=0)
{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>%d</FuncFlag>
</xml>";
$resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);
return $resultStr;
}

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;
}
}
}

?>

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

MySQLi_BAE.class.php

<?php
require_once 'includes/configure.php';

class MySQLi_BAE{

private $mysqli;
private $host;
private $user;
private $password;
private $port;
private $database;

//在类之外访问私有变量时使用
function __get($property_name){
if(isset($this->$property_name)){
return($this->$property_name);
}else{
return(NULL);
}
}

function __set($property_name, $value){
$this->$property_name=$value;
}

function __construct(){

/*从平台获取查询要连接的数据库名称*/
$this->database = MYSQLNAME;

/*从环境变量里取出数据库连接需要的参数*/
$this->host = getenv('HTTP_BAE_ENV_ADDR_SQL_IP');
$this->user = getenv('HTTP_BAE_ENV_AK');
$this->password = getenv('HTTP_BAE_ENV_SK');
$this->port = getenv('HTTP_BAE_ENV_ADDR_SQL_PORT');

$this->mysqli = new mysqli($this->host, $this->user, $this->password, $this->database, $this->port);
if($this->mysqli->connect_error){
die("Connect Server Failed:".$this->mysqli->error);
}

$this->mysqli->query("set names utf8");
}

//dql statement
function execute_dql($query){

$res = $this->mysqli->query($query) or die("操作失败".$this->mysqli->error);
return $res;

//$this->mysqli->close();
}

//dml statement
function execute_dml($query){

$res = $this->mysqli->query($query) or die("操作失败".$this->mysqli->error);

if(!$res){
return 0;//失败
}else{
if($this->mysqli->affected_rows > 0){
return 1;//执行成功
}else{
return 2;//没有行受影响
}
}

//$this->mysqli->close();
}
}
?>

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

configure.php

<?php
header("Content-type:text/html;charset=utf-8");

/***配置数据库名称***/
define("MYSQLNAME", "mzdly");

?>

你可能感兴趣的:(mysql)