当前位置:Gxlcms > PHP教程 > PHP从MySQL数据库读出数据用在微信大众号上

PHP从MySQL数据库读出数据用在微信大众号上

时间:2021-07-01 10:21:17 帮助过:19人阅读

PHP从MySQL数据库读出数据用在微信公众号上
本帖最后由 u014294325 于 2015-06-19 09:51:33 编辑

做微信公众平台 在php页面从数据库中读取数据,用在微信公众平台的关注自动回复上
求指教怎么做

public function handleEvent($postObj)
{

//header('Content-type:text/html;charset=utf-8'); //定义
输出格式编码为utf-8
$db = new mysqli('127.0.0.1','root','123','weixin'); //建立mysql数据库连接
$_ROWS = $db->query('SELECT * FROM event_subscribe'); //用sql语句获取数据
//mysql_query("set names utf8");//设置编码utf8
while($_ROW = $_ROWS->fetch_assoc())
//$title= $_ROW->ess_Title;
//$Description= $_ROW->ess_Description;
//$PicUrl= $_ROW->ess_PictureUrl;
//$Url= $row->ess_Url;

$title= $_ROW['ess_Title'];
$Description= $_ROW['ess_Description'];
$PicUrl= $_ROW['ess_PictureUrl'];
$Url= $row['ess_Url'];
//$contentStr = "欢迎关注哟";
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$msgType = "news";
$time = time();
$textTpl = "


%s

1


<![CDATA[%s]]>





1
";

$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,$title,$Description,$PicUrl,$Url);
echo $resultStr;
------解决思路----------------------
新的代码

public function handleEvent($postObj)
{

//header('Content-type:text/html;charset=utf-8'); //定义
输出格式编码为utf-8
$db = new mysqli('127.0.0.1','root','123','weixin'); //建立mysql数据库连接
$db->query("SET NAMES utf8");//防止乱码
$_ROWS = $db->query('SELECT * FROM event_subscribe where IsPublic=1'); //用sql语句获取数据
$_ROW = mysqli_fetch_assoc($_ROWS);
$textTpl = "


%s

1


<![CDATA[%s]]>





1
";

$resultStr = sprintf($textTpl, $postObj->FromUserName, $postObj->ToUserName, time(),$_ROW['ess_Title'],$_ROW['ess_Description'],$_ROW['ess_PictureUrl'],$_ROW['ess_Url']);

return $resultStr;
}

人气教程排行