Noob Query Question
Moderator: General Moderators
Noob Query Question
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!
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!
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: Noob Query Question
I say write you code exactly as you asked the question...
Does that make sense?
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;
}
?>Re: Noob Query Question
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
It isn't efficient.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.
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));
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: Noob Query Question
Duh, I hella dropped the ball on that one. Yes, us WHERE IN(); Sorry about that.
Re: Noob Query Question
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
That works perfectly, thanks MarkMark Baker wrote:It isn't efficient.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.
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
Mark Baker wrote:It isn't efficient.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.
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