1

Topic: Database username, id lookup

Quick question. Which would be the preferred method for matching uid's to usernames.

1) Select all id's and names into an array and sort through the array.

2) Make a separate lookup on the db for each uid and username?


Cheers,

Matt

Re: Database username, id lookup

What exactly are you trying to do?

3 (edited by MattF 2007-04-22 13:02)

Re: Database username, id lookup

For each subdir in one parent directory, (each subdir being owned by a different user), show the username rather than just the folder name or uid.

Edit: Come to think of it, would it be preferable to create the directory initially with the username rather than with a basic preset dirname appended by the uid?

4

Re: Database username, id lookup

Also, just working on the create dir as username theory, can register.php be easily enough modded to disallow special characters in usernames, if dirname as username is the preferable way of doing things?

Re: Database username, id lookup

So you're trying to read through the folders in a directory and map the user IDs to a username?
Like you said, special characters are the issue: I would suggest just grabbing the id, username for every user and adding it to an array
so $user[1] = 'Guest', $user[2] = 'MattF', etc

6

Re: Database username, id lookup

Smartys wrote:

So you're trying to read through the folders in a directory and map the user IDs to a username?
Like you said, special characters are the issue: I would suggest just grabbing the id, username for every user and adding it to an array
so $user[1] = 'Guest', $user[2] = 'MattF', etc

Cheers. Single database query and sort it from there it is, then. big_smile

Just as a final 'don't want to miss something obvious' type question, is there a maximum size that an array can be, or should it easily hold all the id's and usernames?


Thanks once again,

Matt

Re: Database username, id lookup

Maximum size is the same as the int size on your system I think.

Re: Database username, id lookup

elbekko wrote:

Maximum size is the same as the int size on your system I think.

I think it's limited only by the memory your scripts are allocated wink

9

Re: Database username, id lookup

Just realised I have no idea what the output of the lookup would look like, big_smile, so another question if I may.

$db->query("SELECT id, username FROM ".$db->prefix."users");

What would the actual output look like? Apologies for asking so many questions. smile


Matt

10

Re: Database username, id lookup

Smartys wrote:
elbekko wrote:

Maximum size is the same as the int size on your system I think.

I think it's limited only by the memory your scripts are allocated wink

Right. Numpty mode on. big_smile What's a theoretical guess, (username, id), for a standard setup, then? The last two answers completely befuddled me. big_smile

Re: Database username, id lookup

MattF wrote:

Just realised I have no idea what the output of the lookup would look like, big_smile, so another question if I may.

$db->query("SELECT id, username FROM ".$db->prefix."users");

What would the actual output look like? Apologies for asking so many questions. smile


Matt

You would have to use $db->fetch_assoc($result) or $db->fetch_row($result) to get at the actual data

MattF wrote:
Smartys wrote:
elbekko wrote:

Maximum size is the same as the int size on your system I think.

I think it's limited only by the memory your scripts are allocated wink

Right. Numpty mode on. big_smile What's a theoretical guess, (username, id), for a standard setup, then? The last two answers completely befuddled me. big_smile

The answer? Big enough that you shouldn't worry smile

12

Re: Database username, id lookup

Smartys wrote:

You would have to use $db->fetch_assoc($result) or $db->fetch_row($result) to get at the actual data

So I assume that would mean it's getting back to multiple database lookups, then? Must admit, until I've started looking at this idea, I hadn't realised just quite how convoluted it is. big_smile Php has more features than you can shake a stick at, but it definitely doesn't do anything the easy way. big_smile


MattF wrote:
Smartys wrote:

Right. Numpty mode on. big_smile What's a theoretical guess, (username, id), for a standard setup, then? The last two answers completely befuddled me. big_smile

The answer? Big enough that you shouldn't worry smile

Cheers. That's more the type of answer I understand. big_smile

Re: Database username, id lookup

MattF wrote:
Smartys wrote:

You would have to use $db->fetch_assoc($result) or $db->fetch_row($result) to get at the actual data

So I assume that would mean it's getting back to multiple database lookups, then? Must admit, until I've started looking at this idea, I hadn't realised just quite how convoluted it is. big_smile Php has more features than you can shake a stick at, but it definitely doesn't do anything the easy way. big_smile

No, it's not very complicated at all and doesn't involve any more database lookups. It sounds like I need to explain the way things work wink
When you do $db->query, it returns a resource. That resource can then be given to certain functions (in PunBB's abstraction layer, $db->fetch_assoc and $db->fetch_row are 2 examples) which will allow you to traverse the data.
Your code using $db->fetch_assoc would look something like

$result = $db->query("SELECT id, username FROM ".$db->prefix."users") or error('Could not get user list', __FILE__, __LINE__, $db->error());

$users = array();
while($user = $db->fetch_assoc($result))
    $users[$user['id']] = $user['username'];

Your code using $db->fetch_row would look something like

$result = $db->query("SELECT id, username FROM ".$db->prefix."users") or error('Could not get user list', __FILE__, __LINE__, $db->error());

$users = array();
while(list($id, $username) = $db->fetch_row($result))
    $users[$id] = $username;

Re: Database username, id lookup

Trying to prod 10,000,000 values in an array.
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 35 bytes) in D:\Program Files\xampp\htdocs\punBB4\arr_test.php on line 5

Yep, Smartys is right tongue

15

Re: Database username, id lookup

Smartys wrote:

No, it's not very complicated at all and doesn't involve any more database lookups. It sounds like I need to explain the way things work wink
When you do $db->query, it returns a resource. That resource can then be given to certain functions (in PunBB's abstraction layer, $db->fetch_assoc and $db->fetch_row are 2 examples) which will allow you to traverse the data.
Your code using $db->fetch_assoc would look something like

$result = $db->query("SELECT id, username FROM ".$db->prefix."users") or error('Could not get user list', __FILE__, __LINE__, $db->error());

$users = array();
while($user = $db->fetch_assoc($result))
    $users[$user['id']] = $user['username'];

Your code using $db->fetch_row would look something like

$result = $db->query("SELECT id, username FROM ".$db->prefix."users") or error('Could not get user list', __FILE__, __LINE__, $db->error());

$users = array();
while(list($id, $username) = $db->fetch_row($result))
    $users[$id] = $username;

Thanks ever so much for that. smile I *think* I've seen the light now. big_smile So the basic way that it works is that the initial lookup creates the resource info and connection used by the second lookup. The second lookup then allows you to scroll through the data, (using the while loop), assigning the required info from each row from the table to an array, then terminating the db connection once you have scrolled through, and extracted, the required data? Have I grasped the correct gist of how it works?

Thanks ever so much for your help, BTW. Until around three weeks ago I'd never touched php before, so my learning curve does tend to go awry on occasion. big_smile

16

Re: Database username, id lookup

elbekko wrote:
Trying to prod 10,000,000 values in an array.
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 35 bytes) in D:\Program Files\xampp\htdocs\punBB4\arr_test.php on line 5

Yep, Smartys is right tongue

big_smile big_smile big_smile I take it it should be a safe bet then, looking at those numbers. big_smile

Re: Database username, id lookup

MattF wrote:
Smartys wrote:

No, it's not very complicated at all and doesn't involve any more database lookups. It sounds like I need to explain the way things work wink
When you do $db->query, it returns a resource. That resource can then be given to certain functions (in PunBB's abstraction layer, $db->fetch_assoc and $db->fetch_row are 2 examples) which will allow you to traverse the data.
Your code using $db->fetch_assoc would look something like

$result = $db->query("SELECT id, username FROM ".$db->prefix."users") or error('Could not get user list', __FILE__, __LINE__, $db->error());

$users = array();
while($user = $db->fetch_assoc($result))
    $users[$user['id']] = $user['username'];

Your code using $db->fetch_row would look something like

$result = $db->query("SELECT id, username FROM ".$db->prefix."users") or error('Could not get user list', __FILE__, __LINE__, $db->error());

$users = array();
while(list($id, $username) = $db->fetch_row($result))
    $users[$id] = $username;

Thanks ever so much for that. smile I *think* I've seen the light now. big_smile So the basic way that it works is that the initial lookup creates the resource info and connection used by the second lookup. The second lookup then allows you to scroll through the data, (using the while loop), assigning the required info from each row from the table to an array, then terminating the db connection once you have scrolled through, and extracted, the required data? Have I grasped the correct gist of how it works?

Thanks ever so much for your help, BTW. Until around three weeks ago I'd never touched php before, so my learning curve does tend to go awry on occasion. big_smile

Kind of, except the connection is made before any queries and the connection is closed at the end of the page. It stays open throughout the whole page.

18

Re: Database username, id lookup

Just popping up a quick post to say thanks for all the help and advice. smile Have got that part of things working a treat now.  Thanks again.


Matt