我想搜索某個值,我從窗體中獲得,我想我的查詢中有問題。如何在php中使用LIKE查詢
代碼:
$search = $_POST['search'];
$sql="select news_text,news_no,title_of_the_new from news where news_text like %'$search'%";
誰能幫助?
我想搜索某個值,我從窗體中獲得,我想我的查詢中有問題。如何在php中使用LIKE查詢
代碼:
$search = $_POST['search'];
$sql="select news_text,news_no,title_of_the_new from news where news_text like %'$search'%";
誰能幫助?
兩個問題:
1)百分號需要去你的單引號內:如此,like '%$search%'"
代替like %'$search'%"
2)你需要使用綁定變量是而非直接插傳入search
值轉化爲您的查詢。您正在使用此方法公開自己的SQL注入攻擊。所以,like '?'
,隨後bind
,與該參數值等於'%' + $search + '%'
2)不正確。查詢應該像'?'並且通過綁定傳遞的字符串需要任何一方的%。 –
謝謝你的收穫,@PeteR。現在修復。 – Palpatim
請嘗試以下
$search = $_POST['search'];
$sql="select news_text,news_no,title_of_the_new from news where news_text like '%$search%'";
歡迎來到sqlinjection的世界 –
$ search = mysql_real_escape_string($ _ POST ['searh']); –
'% '$搜索' %'應爲' '%$搜索%''。此外,你應該逃避。如果我搜索''; DROP TABLE新聞; - ?? –
「php查詢」...是的... – akashivskyy
它與PHP無關。 – Havenard