php access以漢語言作為參數進行資料查詢時出現亂碼

來源:互聯網
上載者:User
php access以中文作為參數進行資料查詢時出現亂碼。
先上代碼:

include_once("Conn.php");

$testJSON=array();
$str = trim($_GET['dept']);

$mysql =" select *,0 as ord from checkpro where ifstop=0 and dept like '*".$str."*' ";

echo $mysql;

$rs = $conn->Execute($mysql);
while(!$rs->EOF)
{

$mydept =iconv('gb2312','utf-8',$rs->Fields['dept']->Value);
$mypro =iconv('gb2312','utf-8',$rs->Fields['pro']->Value);
$mynote =iconv('gb2312','utf-8',$rs->Fields['note']->Value);
$mykf =iconv('gb2312','utf-8',$rs->Fields['kf']->Value);

echo $mydept;

$testJSON[] = array('dept'=>"".$mydept."","pro"=>"".$mypro."","note"=>"".$mynote."","kf"=>"".$mykf."");
$rs->MoveNext();
}

foreach ( $testJSON as $key => $value ) {

foreach ( $testJSON[$key] as $key_k => $value_k ) {
$testJSON[$key][$key_k] = urlencode ($value_k);
}
}
echo urldecode ( json_encode ( $testJSON ) );

/*釋放資源*/
$rs->Close();
$conn->Close();
$rs = null;
$conn = null;

?>


如果我在地址欄輸入:
http://192.168.0.30:81/fkfs/jsons_a/json_deptchpro.php?dept=辦公室
那麼就會出現的錯誤,如果參數是英文的就不會。


如果我在地址欄輸入:
http://192.168.0.30:81/fkfs/jsons_a/json_deptchpro.php?dept=通用
這時不會出現錯誤,這個參數得到的結果應該是正確的,但是卻不顯示任何記錄。
問大家,這個該怎麼解決,謝謝!
------解決方案--------------------
錯誤資訊顯示:出錯行是 21 行
請指出 21 行在哪裡!
你貼出的代碼中,21 行是 echo $mydept;,顯然不是的

你對輸出的資料做了編碼轉換(iconv('gb2312','utf-8',$rs->Fields['dept']->Value))
顯然頁面是 utf-8 的
那麼 $_GET['dept'] 也就是 utf-8 的了
據此,你不可能尋找到中文相關的結果
------解決方案--------------------
$str = iconv('utf-8', 'gbk', trim($_GET['dept']));
$mysql =" select *,0 as ord from checkpro where ifstop=0 and dept like '*".$str."*' ";
$rs = $conn->Execute($mysql);
------解決方案--------------------
另外
$mysql =" select *,0 as ord from checkpro where ifstop=0 and dept like '*".$str."*' ";
為什麼是 *
應該
$mysql =" select *,0 as ord from checkpro where ifstop=0 and dept like '%".$str."%' ";
才對
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.