PDA

View Full Version : Small problem with LIMIT


KTBleeding
03-02-2006, 04:34 AM
I'm trying to get the last six members to join my forums to show up on the forum home.. I have the basic idea working now, but for some reason the LIMIT isn't working for me.. It's only returning one result.. AKA the last registered member, rather than the last six.


require_once(DIR . '/includes/functions_user.php');

$newest_members_query = "SELECT userid, username, joindate
FROM " . TABLE_PREFIX . "user
ORDER BY userid DESC
LIMIT 6";

$new_members = $vbulletin->db->query_first($newest_members_query);

if($new_members[userid]){

$new_members[avatarurl] = fetch_avatar_url($new_members[userid]);

if (!$new_members[avatarurl]) {

$new_members[avatarurl] = $stylevar['imgdir_misc'] . '/noavatar.gif';

} else {

$new_members[avatarurl] = $vbulletin->options['bburl'] . '/' . $new_members[avatarurl][0];

}
}

eval('$newest_members .= "' . fetch_template('newest_members') . '";');


Any ideas? I'm stumped. :ermm:
Thanks,

Marco van Herwaarden
03-02-2006, 07:37 AM
$new_members = $vbulletin->db->query_first($newest_members_query);


query_first will only retrieve 1 row.
You will need to use query_read, and then a while loop with fetch_array to loop through the results.

KTBleeding
03-02-2006, 03:49 PM
Ah, I see..

Okay, I tried this new approach out but I'm still getting only one result (I have two registered users right now as I'm developing the site..), only user id2 is showing up..


require_once(DIR . '/includes/functions_user.php');

// Gather the six newest members to join
$newest_members = $vbulletin->db->query_read("SELECT userid, username, joindate
FROM " . TABLE_PREFIX . "user
ORDER BY userid DESC
LIMIT 6");

while ($new_members = $vbulletin->db->fetch_array($newest_members))
{

// Get users avatar
if($new_members[userid])
{
$new_members[avatarurl] = fetch_avatar_url($new_members[userid]);
if (!$new_members[avatarurl]) {
$new_members[avatarurl] = $stylevar['imgdir_misc'] . '/noavatar.gif';
} else {
$new_members[avatarurl] = $vbulletin->options['bburl'] . '/' . $new_members[avatarurl][0];
}
}

eval('$newest_members .= "' . fetch_template('newest_members') . '";');
}


There's also a weird, "Resource id #21" that is being displayed now above the user..

Any ideas??

Thanks, Marco

Marco van Herwaarden
03-03-2006, 06:37 AM
You are using the same variable name $newest_members for both the SQL-resource and the variable to hold you template output.

KTBleeding
03-03-2006, 02:58 PM
That did it.. haha.. man, PHP is a tricky thing..

Thanks a lot Marco! :)

Marco van Herwaarden
03-05-2006, 09:24 AM
Nahh PHP is straight forward, the tricky thing is the coder ;)