0

I'm attempting to pull several users from a database based on what's stored in a specific column using an IN statement.

The users are stored in a column separated by "~" which is why I use str_replace to remove it so it's just numbers. I tried it using a comma instead and that didn't work either. For example, users 1,2 and 3 would be stored in the column as "1~2~3". $stripSymbol is working and properly displays the users, so I'm confused as to why I can't get the sql statement to work.

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
            }
        }

Here's my updated code. It's only displaying the last value in the column.

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
            }
        }
4
  • Let's do this print_r($requested) and then before the query print_r($stripSymbol) and then after the mysql_query if(!$userInvites) { echo mysql_error(); } Feb 11, 2012 at 16:48
  • print_r($requested) outputs 3~2~ and print_r($stripSymbol) outputs '3', '2', " Feb 11, 2012 at 17:02
  • Lets remove the ~ in the end. You could add $requested = trim($requested, '~') before you call explode Feb 11, 2012 at 17:07
  • You should still add if(!$userInvites) { echo $q . mysql_error(); } just before mysql_fetch_array to see the error from MySQL php.net/manual/en/function.mysql-error.php Feb 11, 2012 at 17:09

2 Answers 2

0

This is your code corrected:

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
                }
            }
7
  • Sorry, guess I didn't explain it clearly enough. There are multiple users stored in $stripSymbol that I want to look up so I'm trying to have multiple rows returned. Feb 11, 2012 at 16:19
  • Ok look, insetad of this command: $stripSymbol = str_replace("~","",$requested); try: $stripSymbol = explode("~",$requested); $stripSymbol = "'" . implode("', '", $stripSymbol) . "'"; and it'll work.
    – Naim Zard
    Feb 11, 2012 at 16:29
  • Thanks for the tip, but it's only displaying the last value in the column. Feb 11, 2012 at 16:38
  • Forgot to say when I echo $stripSymbol it shows two values but when I echo the sql statement only 1 value is shown. Also, I updated my code in the original post so you can see what I currently have. Feb 11, 2012 at 16:45
  • I updated the post to answer your last question. check it out now.
    – Naim Zard
    Feb 11, 2012 at 16:46
0

You should change the replace function:

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

Now it replaces "~" with ",".

Add some debugging information:

 $userInvites = mysql_query("select userid,username from users where userid IN ($stripSymbol)") or die($q . " " . mysql_error());
 $userArray = mysql_fetch_array($userInvites);
3
  • I tried that and get the following error Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in... Feb 11, 2012 at 15:51
  • Add the debugging code and we will see what exactly is the mysql error. Feb 11, 2012 at 15:55
  • mysql_fetch_array returns that warning because the prev function mysql_query() failes and returns false. So, i edited my answer and added mysql_query() or die($q . mysql_error()) to see the error thrown by MySQL, not by PHP. Feb 11, 2012 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.