Jump to content

Retreiving User Details


kensbeijing
 Share

Recommended Posts

HiI'm creating a members login for my website, but I have a few questions. This is the first time that I'm doing this so I don't know what's the fastest and most efficient way.I'm using mysql_fetch_array to fetch the rows of details from the user table. What I have at the moment, is the same command on all the pages that I have "included" that needs the user details. So, on one single page I have about 2-3 of the same command.Would a better way be to include a separate page which uses the mysql_fetch_array command, and takes all the the rows and records them down as local variables e.g. $user_name, $user_id. So I can just use the local variables for my whole page?Is there a better way of doing this?

Edited by kensbeijing
Link to comment
Share on other sites

I use a class for that, you may want to look into something similar. It doesn't need to be a class, you could just use an array. Have an include file that checks for a session or however you control who's logged in, and have it lookup everything in the database and populate an array or something. It's better to use an array versus individual variables because it reduces the likelihood of a naming conflict.

Link to comment
Share on other sites

I use a class for that, you may want to look into something similar. It doesn't need to be a class, you could just use an array. Have an include file that checks for a session or however you control who's logged in, and have it lookup everything in the database and populate an array or something. It's better to use an array versus individual variables because it reduces the likelihood of a naming conflict.
OK, that sounds like a great idea. Would I have to use explode to separate each row? Or is there a way to fetch_array into an array so that each row can be retreived like $array[0] (for first row) etc.?
Link to comment
Share on other sites

It's probably best to do it manually, get each row and do whatever you want with it. If you have several rows that you want to add to an array of all rows, you can just push each array onto the end:

$rows = array();$row = array(  'name' => 'Joe',  'id' => 10);$rows[] = $row;$row = array(  'name' => 'John',  'id' => 11);$rows[] = $row;print_r($rows);

If you only have one row or a collection of things to add, you could just add them individually.

$user_data = array(  'name' => '',  'id' => 0);$result = mysql_query(...);if ($row = mysql_fetch_assoc($result)){  $user_data['name'] = $row['name'];  $user_data['id'] = $row['id'];}

Link to comment
Share on other sites

  • 1 month later...

Three queries isn't too bad, as long as they aren't super-complex. If you timed them, you would probably find each query only takes around 0.001 seconds. It isn't so good to store member details in the session, because then you have two sets of data to maintain.

Edited by Synook
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...