2012-08-13 57 views
0

所以我有一個查詢,我將所有的項目都返回到mysql_fetch_array。現在,我知道我可以編寫另一個查詢,只需將我需要的項目選擇到一個單獨的查詢中,但是,是否有一種方法可以從較大的查詢中篩選我想要的$ _GET?

因此,在英語的用戶來自於有一個超鏈接?ID = 1和I [執行一個而獲得的所有值,但是,只有在列表中顯示的$ _GET [「身份證」]項目

<?php //give ma all values but only echo out list of the $_GET['id'] in the url 
    while ($row = mysql_fetch_array($result) { 
    $id = $rowvideo["id"]; 
    $title = $rowvideo["title"]; 
    $length = $rowvideo["length"]; 
} 
echo("<li><a href='#'>". $title." " .$length. "</a></li>"); 
?> 

希望這是有道理的。謝謝你們。

+0

我在這裏看到SQL注入的一個主要漏洞。 – 2012-08-13 15:20:59

+0

htmlentries($ _GET)呢? – pcproff 2012-08-13 15:21:26

+5

// @ RichardJ.RossIII真的嗎?我根本沒有看到任何查詢。 – 2012-08-13 15:21:28

回答

1

如果你不想第二查詢來獲取正是你需要的,在循環一個簡單 - if-statement應該工作:

<?php 
$getId = isset($_GET['id']) ? $_GET['id'] : false; 
//give ma all values but only echo out list of the $_GET['id'] in the url 
while ($row = mysql_fetch_array($result)) { 
    $id = $row["id"]; 
    $title = $row["title"]; 
    $length = $row["length"]; 

    if ($id == $getId) { 
     echo("<li><a href='#'>". $title." " .$length. "</a></li>"); 
    } 
} 
?> 

注意,我宣佈$getId外循環以防止在每次迭代期間使用isset()。如果您不驗證它是否設置並嘗試使用它,則會發出undefined index警告 - 假設您打開了error_reporting(啓用該級別)。

或者,你可以使用PHP的array_filter()上的數據,你已經解析了這一切之後:

$results = array(); 
while ($row = mysql_fetch_array($result)) $results[] = $row; 
if (isset($_GET['id'])) { 
    $filtered = array_filter($results, function($element) use ($_GET['id']) { return ($element['id'] == $_GET['id']); }); 
    $results = $filtered; 
} 
foreach ($results as $result) { 
    echo("<li><a href='#'>". $result['title']." " .$result['length']. "</a></li>"); 
} 

我個人的意見將更有效率,雖然寫的第二個查詢,假設你當然不要當指定id時,實際上並不需要所有的結果。這將是簡單的:

if (isset($_GET['id']) && is_numeric($_GET['id'])) { 
    $query = 'SELECT id, title, length FROM table WHERE id=' . (int)$_GET['id']; 
} else { 
    $query = 'SELECT id, title, length FROM table'; 
} 
// your existing code as-is 
+0

我用這個選項去了。這有什麼安全缺點嗎? – pcproff 2012-08-13 17:42:20

+0

@MIguel然而,並不是真正的「缺點」,但是將數據輸出到HTML的方式存在問題,很容易導致跨站點腳本(XSS)攻擊。你可以通過使用['htmlentities()'](http:// php。net/htmlentities)或['htmlspecialchars()'](http://php.net/htmlspecialchars)給出了你使用它們的確切方式。我不知道你的查詢如何獲取結果,所以我不能說有關SQL注入 - 然而,我的示例查詢會阻止它(儘管額外的輸入驗證不會幫助)。 – newfurniturey 2012-08-13 18:24:10

0
<?php //give ma all values but only echo out list of the $_GET['id'] in the url 
    while ($row = mysql_fetch_array($result)) { 
    $id = $rowvideo["id"]; 
    $title = $rowvideo["title"]; 
    $length = $rowvideo["length"]; 
    if ($id == $_GET['id']) { // or even === 
     echo("<li><a href='#'>". $title." " .$length. "</a></li>"); 
    } 
    } 
?> 
+0

你已經不匹配你的括號$結果))< - 這裏 – Waygood 2012-08-13 15:41:22

+0

@Waygood他直接從OP的代碼複製+粘貼 - 也存在錯誤。 – newfurniturey 2012-08-13 15:48:09

+0

感謝您的提示,編輯。 – rollstuhlfahrer 2012-08-13 16:59:45

0

一點更清晰的位置:

這將允許通過ID在URL過濾器通過指定ID = XXX,XXX爲一個整數,它是積極的。所以,「鮑勃」或-1的ID不會過濾結果仍然給所有結果

$filter=false; 
if(isset($_GET['id'])) 
{ 
    $filter_id=intval($_GET['id']); 
    if($id>0) $filter=true; 
} 

while($row = mysql_fetch_array($result)) 
{ 
    if((!$filter) || (($filter) && ($filter_id==$row['id']))) 
    { 
     $id = $row["id"]; 
     $title = $row["title"]; 
     $length = $row["length"]; 

     // do other stuff here 
    } 
} 

我也改變$ rowvideo至$行,因爲這是你用來獲取結果的陣列。