1 (edited by ricketh? 2006-10-23 14:48)

Topic: Getting a username with ID

Hi, I'm just messing around really trying to broaden my PHP skills (which are like next to nothing tongue)

anyway, I'm just making a page which will display lots of things about a particular user ( i know that's on the profile but meh tongue ) it is similiar to profile.php in the way that it uses id.

so stats.php?id=2 etc..

I grabe the ID:

$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
if ($id < 2)
    message($lang_common['Bad request']);

But I'm trying to run a query to the DB to get the username for this ID. I thought it was this:

$stats_name = $db->query('SELECT username FROM '.$db->prefix.'users WHERE id='.$id);

Obviously not though, so could somebody help me please? big_smile

EDIT: Btw, when I use this query then use

<?php echo($stats_name) ?>

it returns " Resource id #13 "

Re: Getting a username with ID

You're echoing a MySQL resource. You need to do $db->result() or $db->fetch_assoc() on it and use the resulting string/array smile

I'll give you an example:

$stats_res = $db->query('SELECT username FROM '.$db->prefix.'users WHERE id='.$id);
$stats_data = $db->fetch_assoc($stats_res);
echo $stats_data['username'];