在VS2012裡面設定斷點跟蹤執行,發現請求該php檔案擷取的不是正確的返回字串,而是如下出錯資訊:
Invalid query:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
Whole query:
select id,name from index_activities where top=0
這段資訊是我在PHP的
$result = mysql_query($query);
語句行後面,“if”請求不成功才輸出的,顯然是mysql請求沒成功,且可以確定,調用該PHP檔案是成功的。
但是當我在網頁中用同樣的參數調用同一個PHP檔案時(GET方式),傳回值則是正確的。
我php檔案中擷取傳參是用的
$top = $_REQUEST['top'];
所以POST和GET參數應該都可以同樣擷取的。
不知道可能是哪裡出了問題?用WinInet傳參會造成這個請求字串有什麼異常嗎?
回複討論(解決方案)
資料庫連接正確嗎?
資料庫連接正確嗎?
串連正確啊,不正確就返回unable to connect to mysql了,就不會執行到下面的請求,也不會出現MySQL的請求出錯資訊了。
另有一個同樣的類比HTTP請求,也有參數,返回就是正確的:
void CDllValidateDlg::ValidateAPerson(char* Name, char* Code){CString post_data;post_data.Format("userid='%s'&name='%s'",Code,Name); //請求的附加參數CString result; //返回的結果CString post_page = "test_id_validater/validateid.php"; //請求的phpPostHttpPage(result,post_page,post_data);AfxMessageBox(result);}
而我現在調試不明白的這個HTTP請求,到底哪裡不一樣,我把原本由變數決定的參數都寫死了,仍然返回說mysql請求不正確:
void CDllValidateDlg::getActs(HTREEITEM root){CString post_data="top=0";//char top[10];//itoa(ActivitiesTree.GetItemData(root),top,10);//post_data.Format("top=%s",top);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);AfxMessageBox(result); …… ……
感覺好像是PHP端的問題,像下面這麼寫就返回正確了:
原來我寫的代碼是:
Invalid query:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
Whole query:
select id,name from index_activities where top=0
這個資訊分明是資料庫報的錯!
如果 $query = "select id,name from index_activities where top=0"; 可以
而 $query = "select id,name from index_activities where top=$top"; 不可以
這就表示 $top 無值或不是數字
$top = $_REQUEST['top']; 改為 $top = intval($_REQUEST['top']); 試試