Page 1 of 1

Noob Query Question

Posted: Thu Feb 05, 2009 5:11 pm
by jwerre
I'm trying to return an list of users from my DB. Something like this:

getUsers([1234, 5678, 6789]);

function getUsers(&$user_ids){
$result = mysql_query("SELECT* FROM user WHERE ('$user_ids')");
return $result;
}

I'd like to get back an array of users with the same user_ids that I passed in. Any help would be appreciated

Thanks!

Re: Noob Query Question

Posted: Thu Feb 05, 2009 5:18 pm
by RobertGonzalez
I say write you code exactly as you asked the question...

Code: Select all

<?php
/**
 * I'd like to get back an array of users with the same user_ids that I passed in. Any help would be appreciated 
 */
function getUsers($users) {
  global $dbh; // I would seriously recommend not doing this
 
  // Since we have a list of users we will need to loop those in some fashion to get the IDs
  // Assume the array is a one-dimensional array of just IDs
  $ret = array();
 
  foreach ($users as $user) {
    $sql = "SELECT * FROM `userTable` WHERE `userId` = $user";
    if ($result = mysql_query($sql)) {
      $ret[$user] = mysql_fetch_assoc($result);
    } else {
      $ret[$user] = mysql_error();
    }
  }
 
  return $ret;
}
?>
Does that make sense?

Re: Noob Query Question

Posted: Thu Feb 05, 2009 5:54 pm
by jwerre
Is that the best way to do it? Seems like making a bunch of calls to the DB wouldn't be that efficient.

Re: Noob Query Question

Posted: Thu Feb 05, 2009 5:59 pm
by Mark Baker
jwerre wrote:Is that the best way to do it? Seems like making a bunch of calls to the DB wouldn't be that efficient.
It isn't efficient.

Code: Select all

 
function getUsers($users) {
  $sql = "SELECT * FROM `userTable` WHERE `userId` IN (".implode(',',$users).")";
  return mysql_query($sql)) ;
}
 
$users = getUsers(array(101,202,305));
 

Re: Noob Query Question

Posted: Thu Feb 05, 2009 6:06 pm
by RobertGonzalez
Duh, I hella dropped the ball on that one. Yes, us WHERE IN(); Sorry about that.

Re: Noob Query Question

Posted: Thu Feb 05, 2009 6:12 pm
by Benjamin
Errr

Code: Select all

 
function getUsers($users) {
  $resource = mysql_query("SELECT * FROM `userTable` WHERE `userId` IN (".implode(',',$users).")")) ;
  $data = array();
  while ($x = mysql_fetch_assoc($resource)) {
    $data[$x['userId']] = $x;
  }
  return $data;
}
 

Re: Noob Query Question

Posted: Thu Feb 05, 2009 6:17 pm
by jwerre
Mark Baker wrote:
jwerre wrote:Is that the best way to do it? Seems like making a bunch of calls to the DB wouldn't be that efficient.
It isn't efficient.

Code: Select all

 
function getUsers($users) {
  $sql = "SELECT * FROM `userTable` WHERE `userId` IN (".implode(',',$users).")";
  return mysql_query($sql)) ;
}
 
$users = getUsers(array(101,202,305));
 
That works perfectly, thanks Mark

Re: Noob Query Question

Posted: Tue Nov 02, 2010 4:54 pm
by jwerre
Mark Baker wrote:
jwerre wrote:Is that the best way to do it? Seems like making a bunch of calls to the DB wouldn't be that efficient.
It isn't efficient.

Code: Select all

 
function getUsers($users) {
  $sql = "SELECT * FROM `userTable` WHERE `userId` IN (".implode(',',$users).")";
  return mysql_query($sql)) ;
}
 
$users = getUsers(array(101,202,305));
 

That works perfectly, thanks Mark