2015-09-28 38 views
-5

我需要從預防幫助「注意:數組字符串轉換在」防止從「注意:數組字符串轉換在」

$stmt = $DBH->prepare("SELECT * FROM users WHERE id = :emailid"); 
$stmt->bindParam(':emailid', $emailid, PDO::PARAM_STR); 
$stmt->execute(); 
$result = $stmt->fetchAll();  
foreach($result as $row) 
    { 
    echo $row['id'].'<br />'; 
    echo $row['emailid'].'<br />'; 
    echo $row['password']; 
    } 

Everythink其OK,index.php?id=5

但是當我把index.php?id[]=5

回到我錯誤

Notice: Array to string conversion in 

什麼是最好的w^ay在這種情況下檢查數組; error_reporting(0)不是我想找到優化方法的解決方案。

+3

正是你所期待發生把裏面'GET'參數數組的符號是什麼時候? 'index.php?id [] = 5' –

+1

檢查數組:http://php.net/is_array – Mike

+0

只是測試所有參數,在這種情況下,你可以看到完整的路徑披露... – user3576620

回答

1

不知道你想要這個問題走向哪個方向。但是讓我們假設你想要的 - 出於某種原因 - 讓它工作兩種方式:?id=...?id[]=...&id[]=...

if (!is_array($_GET['id'])) { 
    // just make it an array with one element 
    $ids = array($_GET['id']); 
    // so having a simple type string id is just a special case of the 
    // array case 
} 
else { 
    $ids = $_GET['id']; 
} 

$stmt = $DBH->prepare("SELECT * FROM users WHERE id = :emailid"); 
$stmt->bindParam(':emailid', $emailid, PDO::PARAM_STR); 
foreach($ids as $emailid) { 
    $stmt->execute(); 
    $result = $stmt->fetchAll();  
    foreach($result as $row) { 
     ... 
    } 
} 
+0

btw,在別人指出它之前:還可以創建一個'WHERE ID IN(?,?,?...)'準備好的語句,其準確的參數數量爲count($ IDS )' - 但我不會從那裏開始,但依靠查詢計算器來做好工作,網絡開銷可以忽略不計。如果這些假設是錯誤的,你仍然可以改變它;-) – VolkerK

相關問題