2010-07-26 59 views
0

你好,我被困在一個點,我需要你的幫助。我想爲每個$ _GET參數更改SQL QUERY。我使用的是這樣的:?爲每個ISSET繪製SQL查詢?

<? 
if (isset($_GET['page'])) { 
    $pageno = $_GET['page']; 
} else { 
    $pageno = 1; 
} // if 
$query = mysql_query("SELECT count(id) FROM m3_music_mp3"); 
$query_data = mysql_fetch_row($query); 
$numrows = $query_data[0]; 
$rows_per_page = 30; 
$lastpage  = ceil($numrows/$rows_per_page); 
$pageno = (int)$pageno; 
if ($pageno > $lastpage) { 
    $pageno = $lastpage; 
} // if 
if ($pageno < 1) { 
    $pageno = 1; 
} // if 
$limit = 'ORDER BY id DESC LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page; 
$query = mysql_query("SELECT * FROM m3_music_mp3 $limit"); 
?> 

但這IF music.php頁= 1時$查詢= 「SELECT * FROM m3_music_mp3 ORDER BY ID DESC LIMIT X,X」,但我想要做的是,如果音樂.PHP?字= A &頁面= 1套$查詢= 「SELECT * FROM m3_music_mp3 WHERE標題LIKE '$字%' ORDER BY ID DESC LIMIT X,X」

我試圖

if(isset($_GET['word']) && isset($_GET['page'])) { 
$limit .= 'WHERE....' 
} 

東西喜歡但不起作用。謝謝。和對不起我的英語不好& PHP知識貧乏

回答

0

基本上sAc是正確的。但是你可以減少你的代碼。原因限制心不是可選此schould工作就好了: 編輯改變了代碼位是禁止複製和可測試;)

<? 
if (isset($_GET['page'])) { 
    $pageno = $_GET['page']; 
} else { 
    $pageno = 1; 
} // if 
$limit = ""; 
    if(isset($_GET['word'])) { 
     $word = mysql_real_escape_string($_GET['word']); 

     $limit = " WHERE title LIKE '%$word%'" 
    } 
$query = mysql_query("SELECT count(id) FROM m3_music_mp3" . $limit); 
$query_data = mysql_fetch_row($query); 
$numrows = $query_data[0]; 
$rows_per_page = 30; 
$lastpage  = ceil($numrows/$rows_per_page); 
$pageno = (int)$pageno; 
if ($pageno > $lastpage) { 
    $pageno = $lastpage; 
} // if 
if ($pageno < 1) { 
    $pageno = 1; 
} // if 
$limit .= ' ORDER BY id DESC LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page; 
$query = mysql_query("SELECT * FROM m3_music_mp3 $limit"); 
?> 
+0

好的。這也起作用。謝謝。但現在分頁不能正常工作分頁仍然適用於所有數據而不是多少標題行:S – 2010-07-26 14:09:17

+0

獲取isset字位並將其放置BEFORE $ query = mysql_query(「SELECT count(id)FROM m3_music_mp3」); 更改$ query = mysql_query(「SELECT count(id)FROM m3_music_mp3」); to $ query = mysql_query(「SELECT count(id)FROM m3_music_mp3」。$ limit); – teemitzitrone 2010-07-26 14:14:54

0

它應該是:

if(isset($_GET['word'])) { 
    $word = mysql_real_escape_string($_GET['word']); 

    $limit = " WHERE title LIKE '%$word%' ORDER BY id DESC LIMIT " .($pageno - 1) * $rows_per_page .',' .$rows_per_page; 
} 
else 
{ 
    $limit = " ORDER BY id DESC LIMIT " .($pageno - 1) * $rows_per_page .',' .$rows_per_page; 
} 

$query = mysql_query("SELECT * FROM m3_music_mp3 $limit") or die(mysql_error()); 
+0

感謝您的評論。它工作正常。但我如何增加$ _GET的?是使用elseif(isset)的東西應該工作? – 2010-07-26 13:41:29

0

您的解決方案應該可以正常工作,也許你只需要之前WHERE,像='WHERE ...打印的空間。取出你的最終查詢字符串,看看它出錯的地方。