$result = mysql_query($sql, $link); // 执行查询语句
$res=array();
while($row = mysql_fetch_array($result)){
if(条件){
$res[] = $row["title"].$row["answer"];
}
}
return $res;
?>
用这个之后PHP是对,可是接到微信就变成单独的回复一个array
回复讨论(解决方案)
把数据连接成串,或编码成 json
return json_encode($res);
return json_encode($res);
这个个也不行啊,回复的是乱码
有人会吗,大师吗
在echo json_encode之前,加上:header('Content-type:application/json;charset=utf-8');
在echo json_encode之前,加上:header('Content-type:application/json;charset=utf-8');
$sql = "select * FROM `record` WHERE title like '%$keyword%'";
$result = mysql_query($sql, $link); // 执行查询语句
$res=array();
while($row = mysql_fetch_array($result))
{
$res[]=$row["title"];
}
header('Content-type:application/json;charset=utf-8');
return json_encode($res);
mysql_close($link);
} 这次返回的值是[]
在echo json_encode之前,加上:header('Content-type:application/json;charset=utf-8');
会不会是这个有错误?
在echo json_encode之前,加上:header('Content-type:application/json;charset=utf-8');
$sql = "select * FROM `record` WHERE title like '%$keyword%'";
$result = mysql_query($sql, $link); // 执行查询语句
$res=array();
while($row = mysql_fetch_array($result))
{
$res[]=$row["title"];
}
header('Content-type:application/json;charset=utf-8');
return json_encode($res);
mysql_close($link);
} 这次返回的值是[]
返回值为空?那么直接return "hello world";是什么结果?
在echo json_encode之前,加上:header('Content-type:application/json;charset=utf-8');
会不会是这个有错误?
你把
另外:要返回信息,不应该用return,而是echo
public function responseMsg(){ //get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //接收微信发来的XML数据 //extract post data if(!empty($postStr)){ //解析post来的XML为一个对象$postObj $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; //请求消息的用户 $toUsername = $postObj->ToUserName; //"我"的公众号id $keyword = trim($postObj->Content); //用户发送的消息内容 $time = time(); //时间戳 $msgtype = 'text'; //消息类型:文本 $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $contentStr = "输入-h查看帮助吧(=・ω・=)"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStr); echo $resultStr;
在echo json_encode之前,加上:header('Content-type:application/json;charset=utf-8');
$sql = "select * FROM `record` WHERE title like '%$keyword%'";
$result = mysql_query($sql, $link); // 执行查询语句
$res=array();
while($row = mysql_fetch_array($result))
{
$res[]=$row["title"];
}
header('Content-type:application/json;charset=utf-8');
return json_encode($res);
mysql_close($link);
} 这次返回的值是[]
返回值为空?那么直接return "hello world";是什么结果?
抱歉哈,刚才评论错了,
完整的是这个样子的,我压根不知道哪错了
/**
* wechat php test
*/
//define your token
function chaxun($keyword){
$dbname = 'app_yqweixiaoxi';
//$host = getenv('HTTP_BAE_ENV_ADDR_SQL_IP');
//$port = getenv('HTTP_BAE_ENV_ADDR_SQL_PORT');
//$user = getenv('HTTP_BAE_ENV_AK');
//$pwd = getenv('HTTP_BAE_ENV_SK');
/*接着调用mysql_connect()连接服务器*/
$link = mysql_connect ( SAE_MYSQL_HOST_M . ':' . SAE_MYSQL_PORT, SAE_MYSQL_USER, SAE_MYSQL_PASS );
if(!$link) {
die("Connect Server Failed: " . mysql_error($link));
}
/*连接成功后立即调用mysql_select_db()选中需要连接的数据库*/
if(!mysql_select_db($dbname,$link))
{
die("Select Database Failed: " . mysql_error($link));
}
mysql_query("set names GBK",$link);
$sql = "select * FROM `record` WHERE title like '%$keyword%'";
$result = mysql_query($sql, $link); // 执行查询语句
$res=array();
while($row = mysql_fetch_array($result))
{
$res[]=$row["title"];
}
header('Content-type:application/json;charset=utf-8');
return json_encode($res);
mysql_close($link);
}
define("TOKEN", "YQweixiaoxi");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
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 = "
if(!empty($keyword ))
{
$msgType = "text";
$contentStr= chaxun($keyword);
$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;
}
}
}
?>
在echo json_encode之前,加上:header('Content-type:application/json;charset=utf-8');
$sql = "select * FROM `record` WHERE title like '%$keyword%'";
$result = mysql_query($sql, $link); // 执行查询语句
$res=array();
while($row = mysql_fetch_array($result))
{
$res[]=$row["title"];
}
header('Content-type:application/json;charset=utf-8');
return json_encode($res);
mysql_close($link);
} 这次返回的值是[]
返回值为空?那么直接return "hello world";是什么结果?
在echo json_encode之前,加上:header('Content-type:application/json;charset=utf-8');
$sql = "select * FROM `record` WHERE title like '%$keyword%'";
$result = mysql_query($sql, $link); // 执行查询语句
$res=array();
while($row = mysql_fetch_array($result))
{
$res[]=$row["title"];
}
header('Content-type:application/json;charset=utf-8');
return json_encode($res);
mysql_close($link);
} 这次返回的值是[]
返回值为空?那么直接return "hello world";是什么结果?
数据库的是这样的
你的表字段是UTF-8,所以应该 mysql_query("set names utf8",$link);
另外,有个地方稍微改下(改不改都行,不过echo的数据都应该是XML格式的):
if(!empty($keyword )){ $msgType = "text"; $contentStr = chaxun($keyword); $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr;}else{ $msgType = "text"; $contentStr = "input something..."; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr;}
你的表字段是UTF-8,所以应该 mysql_query("set names utf8",$link);
另外,有个地方稍微改下(改不改都行,不过echo的数据都应该是XML格式的):
if(!empty($keyword )){ $msgType = "text"; $contentStr = chaxun($keyword); $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr;}else{ $msgType = "text"; $contentStr = "input something..."; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr;}
都改了,还是不行,烦死了,用return返回array,用echo 回复公众号暂停服务
1、你是在做微信应用,而微信都是 utf-8 编码的
所以你 mysql_query("set names GBK",$link); 是不对的
要 mysql_query("set names utf8",$link);
2、你把传入的 utf-8 数据 $keyword 当做 gbk 解释
这就造成了 chaxun 函数返回空数组
当然确实不存在的时候,也是返回空数组的
3、由
$contentStr= chaxun($keyword);
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
可知,$contentStr 应是一个字符串,而 chaxun 返回的是数组,所以可能需要
$contentStr = json_encode(chaxun($keyword));
或
$contentStr = join(',', chaxun($keyword));
1、用echo 回复公众号暂停服务====》可能是代码有语法错误或者error,在你的域名里运行这个脚本看看有没有报错。
2、试一下,不进行查找数据库,直接让$contentStr = "字符串";然后echo $resultStr; 看看是不是数据取出问题
1、用echo 回复公众号暂停服务====》可能是代码有语法错误或者error,在你的域名里运行这个脚本看看有没有报错。
2、试一下,不进行查找数据库,直接让$contentStr = "字符串";然后echo $resultStr; 看看是不是数据取出问题
问题解决了,谢谢你,大师傅
1、你是在做微信应用,而微信都是 utf-8 编码的
所以你 mysql_query("set names GBK",$link); 是不对的
要 mysql_query("set names utf8",$link);
2、你把传入的 utf-8 数据 $keyword 当做 gbk 解释
这就造成了 chaxun 函数返回空数组
当然确实不存在的时候,也是返回空数组的
3、由
$contentStr= chaxun($keyword);
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
可知,$contentStr 应是一个字符串,而 chaxun 返回的是数组,所以可能需要
$contentStr = json_encode(chaxun($keyword));
或
$contentStr = join(',', chaxun($keyword));
大师啊,大师啊,解决了,佩服啊
太感谢了

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.

PHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.