Mac Taylor
Mac Taylor

Reputation: 5148

search an array in mysql table

this is what i am trying to do :

 $allwinnersarra = array(26809,26805,24279,4839,20939,17678,4999,17745,1);

 $results_query = mysql_query("SELECT username FROM table_users WHERE userid IN('".join("','", $allwinnersarra)."')"); 

 while ($row = mysql_fetch_array($results_query)) {
        $usernamesFound.= $row['username'];
 }

 die($usernamesFound);

there is an array of id numbers and I want their usernames , in my own idea my approach is correct but the outcome is not.

The output is only the first username and does not show other usernames

Upvotes: 1

Views: 1476

Answers (2)

web-nomad
web-nomad

Reputation: 6003

Try this:

$allwinnersarra = array(26809,26805,24279,4839,20939,17678,4999,17745,1);
$allwinnersarra = implode( ',', $allwinnersarra );

$sql = "SELECT username FROM table_users WHERE userid IN 
          (" . mysql_real_escape_string( $allwinnersarra ) . ");";

Hope it helps...

Upvotes: 3

peipst9lker
peipst9lker

Reputation: 605

  1. Is $usernamesFound even a string? Please declare it above the loop.
  2. Add a whitespace between the variable name and the .=-operator
  3. I would suggest you putting the usernames in an array

    $users = array();

    while($row = mysql_fetch_array($results_query)) { $users[] = $row['username']; }

  4. Also please use implode instead of join.

Upvotes: 2

Related Questions