A PHP "select from where in" query example

If you ever need the source code for a PHP "SELECT FROM foo WHERE id in (1,2,3)" example, I hope the following PHP function will be a decent example to work from. I turned a given list of database table id values into a CSV list, then use that string in a "SELECT FROM ... WHERE ... IN ... (list)" SQL query.

I'm not going to explain it today, but I will share the PHP source code for your reading enjoyment (and problem solving).

My PHP SELECT FROM ... WHERE ... IN ... (list)" SQL query

Here is the source code for my "SELECT FROM ... WHERE ... IN ... (list)" SQL query:

# returns a list of the tag names for the given list of tag_ids.
function get_tags($tag_ids)
  # create an array to store the results
  $tags = array();

  # convert the array to a csv string for use with the query
  $tag_ids_as_csv = implode(',', $tag_ids);

  # use the csv string in the SELECT FROM WHERE IN query
  $result = mysql_query("SELECT tag FROM tags WHERE id in ($tag_ids_as_csv)");

  # populate the 'tags' array from the results.
  # (there may be a better way to do this)
  $row = mysql_fetch_array($result);
  while ($row = mysql_fetch_array($result, MYSQL_BOTH))
    $tmp = $row['tag'];
    array_push($tags, $tmp);
  # return the tags array
  return $tags;

I can add more information on this PHP "SELECT FROM WHERE IN" query example, but for now I'll let it stand as is. The SQL query highlighted in bold is pretty much the point of this article, showing how to use a CSV array in a PHP SQL "SELECT FROM WHERE IN" query.

As noted, there is probably a better way to handle the array received from the query, but I'm a relatively new PHP SQL query developer, and that's the only way I know to do it right now.