2012-02-11 34 views
0

我試圖從一個數據庫中根據使用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 
      } 
     } 
+0

讓我們做到這一點'的print_r($請求)'然後在查詢之前'的print_r($ stripSymbol)'然後在'mysql_query'後面if(!$ userInvites){echo mysql_error(); } $ – 2012-02-11 16:48:41

+0

print_r($ requested)輸出3〜2〜和print_r($ stripSymbol)輸出'3','2',' – user1104854 2012-02-11 17:02:32

+0

讓我們最終刪除〜你可以添加'$ requested = trim($ requested, '〜')'在你調用'explode'之前' – 2012-02-11 17:07:22

回答

0

您應該更改替換功能:

$stripSymbol = str_replace("~",",",$requested); 

現在它取代 「〜」 與 「」。

添加一些調試信息:

$userInvites = mysql_query("select userid,username from users where userid IN ($stripSymbol)") or die($q . " " . mysql_error()); 
$userArray = mysql_fetch_array($userInvites); 
+0

我試過,並得到以下錯誤警告:mysql_fetch_array()期望參數1是資源,布爾給定在... – user1104854 2012-02-11 15:51:51

+0

添加調試代碼,我們將看到究竟是什麼是mysql錯誤 – 2012-02-11 15:55:45

+0

這是什麼意思?我已經給你了錯誤 – user1104854 2012-02-11 15:58:07

0

這是你的代碼更正:

if(($requested !=="") OR ($requested !=="~")){ 
       $stripSymbol = explode("~", $requested); //$requested is the list of users 
       $stripSymbol = "'" . implode("', '", $requested) . "'"; 
       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); 
      foreach($userArray as $item) 
       { 
        $userN = $item['username']; 
        echo $userN . "<br/>"; //testing to see if it's pulling the values properly, nothing is displayed 
       } 
      } 
+0

對不起,我猜想我沒有足夠清楚地解釋它。有多個用戶存儲在$ stripSymbol中,我想查找所以我試圖返回多行。 – user1104854 2012-02-11 16:19:55

+0

好的看,這個命令的插圖:$ stripSymbol = str_replace(「〜」,「」,$ requested); 嘗試:$ stripSymbol = explode(「〜」,$ requested); $ stripSymbol =「'」。 implode(「','」,$ stripSymbol)。 「'」; 它會工作。 – 2012-02-11 16:29:07

+0

感謝您的提示,但它只顯示列中的最後一個值。 – user1104854 2012-02-11 16:38:37