我試圖從一個數據庫中根據使用IN語句存儲在特定列中的內容拉出幾個用戶。與mysql和php使用IN語句的麻煩
用戶存儲在由「〜」分隔的列中,這就是爲什麼我使用str_replace刪除它,所以它只是數字。我嘗試使用逗號代替,並且也沒有工作。例如,用戶1,2和3將被存儲在列中作爲「1〜2〜3」。 $ stripSymbol正在工作,並正確顯示用戶,所以我很困惑,爲什麼我不能讓sql語句工作。
if($eventBy == $_SESSION['userid']){
if(($requested !=="") OR ($requested !=="~")){
$stripSymbol = str_replace("~","",$requested); //$requested is the list of users
echo $stripSymbol; //used for testing, displays the values properly so I know this works
$userInvites = mysql_query("select userid,username from users where userid IN ($stripSymbol)");
$userArray = mysql_fetch_array($userInvites);
$userN = $userArray['username'];
echo $userN; //testing to see if it's pulling the values properly, nothing is displayed
}
}
這是我的更新代碼。它只顯示列中的最後一個值。
if($eventBy == $_SESSION['userid']){
if(($requested !=="") OR ($requested !=="~")){
$stripSymbol = explode("~",$requested);
$stripSymbol = "'" . implode("', '", $stripSymbol) . "'";
$userInvites = mysql_query("select userid,username from users where userid IN (".$stripSymbol.")");
$userArray = mysql_fetch_array($userInvites);
$userN = $userArray['username'];
echo $userN; //testing to see if it's pulling the values properly, nothing is displayed
}
}
讓我們做到這一點'的print_r($請求)'然後在查詢之前'的print_r($ stripSymbol)'然後在'mysql_query'後面if(!$ userInvites){echo mysql_error(); } $ – 2012-02-11 16:48:41
print_r($ requested)輸出3〜2〜和print_r($ stripSymbol)輸出'3','2',' – user1104854 2012-02-11 17:02:32
讓我們最終刪除〜你可以添加'$ requested = trim($ requested, '〜')'在你調用'explode'之前' – 2012-02-11 17:07:22