Home > Backend Development > PHP Tutorial > PHP从MySQL数据库读出数据用在微信公众号上

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

WBOY
Release: 2016-06-23 13:32:19
Original
1249 people have browsed it

做微信公众平台 在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 = "<xml> 					<ToUserName><![CDATA[%s]]></ToUserName> 					<FromUserName><![CDATA[%s]]></FromUserName> 					<CreateTime>%s</CreateTime>					<MsgType><![CDATA[news]]></MsgType> 					<ArticleCount>1</ArticleCount> 					<Articles> 						<item> 							<Title><![CDATA[%s]]></Title>  							<Description><![CDATA[%s]]></Description> 							<PicUrl><![CDATA[%s]]></PicUrl> 							<Url><![CDATA[%s]]></Url> 						</item> 						 					</Articles> 					<FuncFlag>1</FuncFlag> 				</xml>";				$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,$title,$Description,$PicUrl,$Url);        echo $resultStr;    
Copy after login


回复讨论(解决方案)


没有值传到item内各项
log

2015-06-19 09:20:52 QUERY_STRING:signature=2580385de24285215c55584528a9732b68327681&timestamp=1434676848&nonce=18942184112015-06-19 09:20:52 <xml> 					<ToUserName><![CDATA[oTVfwt8FLtDlTnKOZgiolB-4dI6o]]></ToUserName> 					<FromUserName><![CDATA[gh_265404f8e5f8]]></FromUserName> 					<CreateTime>1434676852</CreateTime>					<MsgType><![CDATA[news]]></MsgType> 					<ArticleCount>1</ArticleCount> 					<Articles> 						<item> 							<Title><![CDATA[]]></Title>  							<Description><![CDATA[]]></Description> 							<PicUrl><![CDATA[]]></PicUrl> 							<Url><![CDATA[]]></Url> 						</item> 						 					</Articles> 					<FuncFlag>1</FuncFlag> 				</xml>
Copy after login

首先,这代码是有很大问题的。
1.数据库查询信息条数,使用while($_ROW = $_ROWS->fetch_assoc())表示循环查询嘛,不知道你数据库中有多少条记录。假设只有一条,那最好。在这里你可以使用var_dump($_ROW),查看下数组结构,再进行赋值。我推荐多条记录使用$_ROW = $_ROWS->fetch_array()的方式,fetch_array()查询结果为二维数组。assoc是一维数组。多条记录用二维,单条记录用一维即可。
2.回复信息类型是图文消息,xml数据包中声明的是单图文,也就只能接收一条数据库记录了。你之前sql查询的没有where条件,还是先var_dump看一下。
3.最后的echo使用时记得改成return。

这个代码中让我最费解的是那个第8行的where。既然使用了where,说明你知道会查询多条记录。如果只有一条记录,那么
1.将sql查询语句设置where条件,保证只能查出一条记录。
2.不使用where,改成if(记得下面的代码用括号括起来),或者干脆去掉where,只保留$_ROW = $_ROWS->fetch_assoc()

如果要组合多图文消息,那么
1.使用do{}---while()循环,因为我之前讲过,while循环会丢失第一条记录(这或许就是你查不到赋值信息的原因)。
2.循环

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 limit 5');                          //用sql语句获取数据   $newsArray = array(); 	do 	{ 		$newsArray[]=array("Title"=>$_ROW['ess_Title'],  "Description"=>$_ROW['ess_Description'], "PicUrl"=>$_ROW['ess_PictureUrl'], "Url" =>$row['ess_Url']); 	}while ($_ROW = $_ROWS->fetch_array()); 	        $itemTpl = "<item>        <Title><![CDATA[%s]]></Title>        <Description><![CDATA[%s]]></Description>        <PicUrl><![CDATA[%s]]></PicUrl>        <Url><![CDATA[%s]]></Url>        </item>";        $item_str = "";        foreach ($newsArray as $item){        	$item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);        }        $newsTpl = "<xml>        <ToUserName><![CDATA[%s]]></ToUserName>        <FromUserName><![CDATA[%s]]></FromUserName>        <CreateTime>%s</CreateTime>        <MsgType><![CDATA[news]]></MsgType>        <ArticleCount>%s</ArticleCount>        <Articles>        $item_str</Articles>        </xml>";                $result = sprintf($newsTpl, $postObj->FromUserName, $postObj->ToUserName, time(), count($newsArray));        return $result;    }  
Copy after login

新的代码

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 = "<xml>                     <ToUserName><![CDATA[%s]]></ToUserName>                     <FromUserName><![CDATA[%s]]></FromUserName>                     <CreateTime>%s</CreateTime>                    <MsgType><![CDATA[news]]></MsgType>                     <ArticleCount>1</ArticleCount>                     <Articles>                         <item>                             <Title><![CDATA[%s]]></Title>                              <Description><![CDATA[%s]]></Description>                             <PicUrl><![CDATA[%s]]></PicUrl>                             <Url><![CDATA[%s]]></Url>                         </item>                                             </Articles>                     <FuncFlag>1</FuncFlag>                 </xml>";                 $resultStr = sprintf($textTpl, $postObj->FromUserName, $postObj->ToUserName, time(),$_ROW['ess_Title'],$_ROW['ess_Description'],$_ROW['ess_PictureUrl'],$_ROW['ess_Url']);                return $resultStr;    }  
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template