在新浪sae上php代码调用数据库
下列代码有什么错吗?想调用数据库啊,谢谢大家,没分了抱歉<?php/***wechatphptest*///defineyourtokendefine("TOKEN","...
下列代码有什么错吗?想调用数据库啊,谢谢大家,没分了抱歉
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
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 = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
$con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);if(!$con){ die('could not connect:'.mysql_error()); }mysql_select_db(SAE_MYSQL_DB,$con);
$sql = "SELECT * FROM `tsg` WHERE `title` = \'合伙人\' LIMIT 0, 30 ";
$query=mysql_query($sql);
$rs=mysql_fetch_array($query);
$contentStr=$rs['content'];
mysql_close($link);
if(!empty( $keyword ))
{
$msgType = "text";
$contentStr = "Welcome to wechat world!";
$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;
}
}
}
?> 展开
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
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 = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
$con = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);if(!$con){ die('could not connect:'.mysql_error()); }mysql_select_db(SAE_MYSQL_DB,$con);
$sql = "SELECT * FROM `tsg` WHERE `title` = \'合伙人\' LIMIT 0, 30 ";
$query=mysql_query($sql);
$rs=mysql_fetch_array($query);
$contentStr=$rs['content'];
mysql_close($link);
if(!empty( $keyword ))
{
$msgType = "text";
$contentStr = "Welcome to wechat world!";
$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;
}
}
}
?> 展开
1个回答
展开全部
<?php
// 连主库
$link=mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
// 连从库
// $link=mysql_connect(SAE_MYSQL_HOST_S.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
if($link)
{
mysql_select_db(SAE_MYSQL_DB,$link);
//your code goes here
}
//创建一个数据库表
function _create_table($sql){
mysql_query($sql) or die('创建表失败,错误信息:'.mysql_error());
return "创建表成功";
}
//插入数据
function _insert_data($sql){
if(!mysql_query($sql)){
return 0; //插入数据失败
}else{
if(mysql_affected_rows()>0){
return 1; //插入成功
}else{
return 2; //没有行受到影响
}
}
}
//删除数据
function _delete_data($sql){
if(!mysql_query($sql)){
return 0; //删除失败
}else{
if(mysql_affected_rows()>0){
return 1; //删除成功
}else{
return 2; //没有行受到影响
}
}
}
//修改数据
function _update_data($sql){
if(!mysql_query($sql)){
return 0; //更新数据失败
}else{
if(mysql_affected_rows()>0){
return 1; //更新成功;
}else{
return 2; //没有行受到影响
}
}
}
function _select_data($sql){
$ret = mysql_query($sql) or die('SQL语句有错误,错误信息:'.mysql_error());
return $ret;
}
function _drop_table($sql){
mysql_query($sql) or die('删除表失败,错误信息:'.mysql_error());
return "删除表成功";
}
?>
// 连主库
$link=mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
// 连从库
// $link=mysql_connect(SAE_MYSQL_HOST_S.':'.SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
if($link)
{
mysql_select_db(SAE_MYSQL_DB,$link);
//your code goes here
}
//创建一个数据库表
function _create_table($sql){
mysql_query($sql) or die('创建表失败,错误信息:'.mysql_error());
return "创建表成功";
}
//插入数据
function _insert_data($sql){
if(!mysql_query($sql)){
return 0; //插入数据失败
}else{
if(mysql_affected_rows()>0){
return 1; //插入成功
}else{
return 2; //没有行受到影响
}
}
}
//删除数据
function _delete_data($sql){
if(!mysql_query($sql)){
return 0; //删除失败
}else{
if(mysql_affected_rows()>0){
return 1; //删除成功
}else{
return 2; //没有行受到影响
}
}
}
//修改数据
function _update_data($sql){
if(!mysql_query($sql)){
return 0; //更新数据失败
}else{
if(mysql_affected_rows()>0){
return 1; //更新成功;
}else{
return 2; //没有行受到影响
}
}
}
function _select_data($sql){
$ret = mysql_query($sql) or die('SQL语句有错误,错误信息:'.mysql_error());
return $ret;
}
function _drop_table($sql){
mysql_query($sql) or die('删除表失败,错误信息:'.mysql_error());
return "删除表成功";
}
?>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询