下面这个是只能网页GET传参,无法VC端POST传参的PHP文件代码,Mysql请求中的$top只能用GET获取的参数。
<?phprequire "use_daoru.php";
$top = $_REQUEST['top'];
$query = "select id,name from index_activities where top= $top";
$result = mysql_query($query);
//if(!$result){
//$message = 'Invalid query: '.mysql_error()."\n";
//$message.= 'Whole query: '.$query;
//die($message);
//}$num = mysql_num_rows($result);
for($i=0;$i<$num;$i++){
$row = mysql_fetch_row($result);
echo($row[0].":".$row[1].",");
}
?>下面代码段是直接用数字0(或其他任意数字)代替了$top。
这样的话,VC模拟POST和网页发送GET就都没有问题,即使它们仍然传递了top参数,只要在mysql查询语句中没有使用,就不会有问题。
<?phprequire "use_daoru.php";
$top = $_REQUEST['top'];
$query = "select id,name from index_activities where top= 0";
$result = mysql_query($query);
//if(!$result){
//$message = 'Invalid query: '.mysql_error()."\n";
//$message.= 'Whole query: '.$query;
//die($message);
//}$num = mysql_num_rows($result);
for($i=0;$i<$num;$i++){
$row = mysql_fetch_row($result);
echo($row[0].":".$row[1].",");
}
?>

解决方案 »

  1.   

    这应该是VC端编入的字符编码有问题吧,传参的时候不会出错,用这个编码不正确的字符“0”,也就是top,请求mysql的时候就出错了。
    但具体哪里出了问题呢?
    VC端的项目编码是multibyte,mysql连接正确后设置了“set names 'utf8'”。
    调用PostHttpPage之前,top的来源是下面这样的:
    void CDllValidateDlg::getActs(HTREEITEM root)
    {
    CString post_data;
    // char top[10];
    // itoa(ActivitiesTree.GetItemData(root),top,10);
    // AfxMessageBox(top);
    // string str_top=MultiByteToUtf8(top);
    // AfxMessageBox(str_top.data());
    post_data.Format("top=%d",
    ActivitiesTree.GetItemData(root));
    CString result;
    CString post_page = "test_id_validater/GetActivities.php";
    // AfxMessageBox("post_page:"+post_page+", "+"post_data:"+post_data);
    PostHttpPage(result, post_page, post_data);
    ……
    ……
    在PostHttpPage函数中,包含top的postData是像下面这样处理的:
    BOOL CDllValidateDlg::PostHttpPage(CString &result,CString &PageName,CString &postData) // 请求Http.
    {
    CInternetSession session("SighAgent");
    try
    {
    INTERNET_PORT nPort = 80;
    DWORD dwRet = 0; /*以下为wininet 网络请求流程*/
    CHttpConnection* pServer = session.GetHttpConnection(SERVER_IP, nPort);
    CHttpFile* pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,PageName); //打开一个请求
    CString strHeaders = "Content-Type: application/x-www-form-urlencoded"; // 请求头 //开始发送请求

    string data_post = MultiByteToUtf8(postData.GetBuffer(0));
    pFile->SendRequest(strHeaders,(LPVOID)data_post.data(),data_post.size()); //发送请求,注意此为堵塞函数
    ……
    ……
    //至此已经将参数发送出去,后面的语句不涉及字符编码转换问题
      

  2.   

    我自己回复吧:这是PHP端把0当成空字符串了。