Noob Query Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jwerre
Forum Newbie
Posts: 18
Joined: Thu Feb 05, 2009 5:06 pm

Noob Query Question

Post 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!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Noob Query Question

Post 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?
jwerre
Forum Newbie
Posts: 18
Joined: Thu Feb 05, 2009 5:06 pm

Re: Noob Query Question

Post 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.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Noob Query Question

Post 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));
 
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Noob Query Question

Post by RobertGonzalez »

Duh, I hella dropped the ball on that one. Yes, us WHERE IN(); Sorry about that.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Noob Query Question

Post 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;
}
 
jwerre
Forum Newbie
Posts: 18
Joined: Thu Feb 05, 2009 5:06 pm

Re: Noob Query Question

Post 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
jwerre
Forum Newbie
Posts: 18
Joined: Thu Feb 05, 2009 5:06 pm

Re: Noob Query Question

Post 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
Post Reply