PHP reads data from the MySQL database and uses the data on the public account. the public platform reads data from the database on the php page, and uses the data on the public platform for automatic follow-up reply.
How can I do this?
Public function handleEvent ($ postObj) {// header ('content-type: text/html; charset = utf-8 '); // define the output format encoded as UTF-8 $ db = new mysqli ('2017. 0.0.1 ', 'root', '123', 'weixin'); // Create a mysql database connection $ _ ROWS = $ db-> query ('select * FROM event_subscribe '); // use an SQL statement to obtain data // mysql_query ("set names utf8"); // set the encoding 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 = "Welcome to our attention "; $ fromUsername = $ postObj-> FromUserName; $ toUsername = $ postObj-> ToUserName; $ msgType = "news"; $ time = time (); $ textTpl ="
%s
%s
% S
news
1
<! [CDATA [% s]>
%s
%s
%s
1
"; $ ResultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ title, $ Description, $ PicUrl, $ Url); echo $ resultStr;
Reply to discussion (solution)
No value is uploaded to items in item
Log
2015-06-19 09:20:52 QUERY_STRING:signature=2580385de24285215c55584528a9732b68327681×tamp=1434676848&nonce=18942184112015-06-19 09:20:52
oTVfwt8FLtDlTnKOZgiolB-4dI6o
gh_265404f8e5f8
1434676852
news
1
<![CDATA[]]>
1
First, this code has a big problem.
1. the number of information items in the database query. use while ($ _ ROW = $ _ ROWS-> fetch_assoc () to indicate the number of records in the database. If there is only one, it is best. Here you can use var_dump ($ _ ROW) to view the array structure and assign values. We recommend that you use the $ _ ROW =$ _ ROWS-> fetch_array () method for multiple records. the fetch_array () query result is a two-dimensional array. Assoc is a one-dimensional array. Multiple records are saved in two-dimensional mode, and a single record is saved in one-dimensional mode.
2. the reply message type is a text message, and the xml data packet declares a single text message, so that only one database record can be received. You didn't have the where condition for your SQL query before. you should check var_dump first.
3. change the last echo value to return.
What puzzles me most in this code is the where of the 8th rows. Since the where clause is used, you know that multiple records will be queried. If there is only one record
1. set the where condition for the SQL query statement to ensure that only one record can be found.
2. do not use where, change it to if (remember to enclose the following code in parentheses), or simply remove where and only keep $ _ ROW = $ _ ROWS-> fetch_assoc ()
.
If you want to combine multiple text messages
1. use the do {} --- while () loop, because as I mentioned earlier, the while loop will lose the first record (this may be the reason you cannot find the value assignment information ).
2. Loop
Public function handleEvent ($ postObj) {// header ('content-type: text/html; charset = utf-8 '); // define the output format encoded as UTF-8 $ db = new mysqli ('2017. 0.0.1 ', 'root', '123', 'weixin '); // Create a mysql database connection $ _ ROWS = $ db-> query ('select * FROM event_subscribe limit 5'); // Obtain data using SQL statements $ 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 ="
<! [CDATA [% s]>
%s
%s
%s
"; $ Item_str =" "; foreach ($ newsArray as $ item) {$ item_str. = sprintf ($ itemTpl, $ item ['title'], $ item ['description'], $ item ['picurl'], $ item ['URL']);} $ newsTpl ="
%s
%s
% S
news
% S$ Item_str
"; $ Result = sprintf ($ newsTpl, $ postObj-> FromUserName, $ postObj-> ToUserName, time (), count ($ newsArray); return $ result ;}
New code
Public function handleEvent ($ postObj) {// header ('content-type: text/html; charset = utf-8 '); // define the output format encoded as UTF-8 $ db = new mysqli ('2017. 0.0.1 ', 'root', '000000', 'weixin'); // Create a mysql database connection $ db-> query ("set names utf8 "); // prevent garbled characters $ _ ROWS = $ db-> query ('select * FROM event_subscribe where IsPublic = 1 '); // Obtain data using SQL statements $ _ ROW = mysqli_fetch_assoc ($ _ ROWS); $ textTpl ="
%s
%s
% S
news
1
<! [CDATA [% s]>
%s
%s
%s
1
"; $ ResultStr = sprintf ($ textTpl, $ postObj-> FromUserName, $ postObj-> ToUserName, time (), $ _ ROW ['ess _ title'], $ _ ROW ['ess _ description'], $ _ ROW ['ess _ PictureUrl '], $ _ ROW ['ess _ url']); return $ resultStr ;}