[Solved] Retrieving user ID

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Devnull
Forum Commoner
Posts: 52
Joined: Fri Oct 22, 2004 2:19 pm

[Solved] Retrieving user ID

Post by Devnull »

I got the following functions and through one of them I need to get the user ID so I can retrieve information for the user's area.

Code: Select all

function get_area() {
 $conn = db_connect();
 if (!$conn)
    return false;

 $result = mysql_query("select province from user
                         where id= '$userid'");

// added a $ before result here
 if (!$result)
    return false;
 if (mysql_num_rows($result)>0)
    return true;
 else
    return false;

// here's a line to extract the row from the db
$array = mysql_fetch_assoc($result);

// store the province in $province
return $array['province'];
}
Basically, what I need is to query the MySQL database and get the user ID, I'm currently using sessions for logging in and out. I don't know where to put this code and what to put it as...

Code: Select all

function login($user, $pass) {

	$conn = db_connect();
	if (!$conn)
		return false;
		
	$result = mysql_query("select * from user
									where user = '$user'
									and pass = MD5('$pass')");
	if (!result)
		return false;
	if (mysql_num_rows($result)>0)
		return true;
	else
		return false;
}

function check_valid_user() {
	
	global $HTTP_SESSION_VARS;
		if (isset($HTTP_SESSION_VARS['valid_user'])) {
			echo '<div class="error">Iniciado sesi&oacute;n como <strong>'.$HTTP_SESSION_VARS['valid_user'].'</strong> (<a href="user.php?a=insert">A&ntilde;adir datos</a> | <a href="user.php?a=view">Mostrar Datos</a> | <a href="logout.php">Desconexi&oacute;n</a>).</div>';
		}
		else {
			echo '<div class="error">Su nombre de usuario o contrase&ntilde;a es incorecto!</div>';
			include('includes/templates/login.html');
			include('includes/templates/footer.html');
			
			exit;
	}
}

feyd |

Code: Select all

tags make it far easier to read larger code blocks, don't you think?[/color]
Last edited by Devnull on Mon Aug 08, 2005 4:23 am, edited 1 time in total.
Sander
Forum Commoner
Posts: 38
Joined: Sat Aug 06, 2005 12:43 pm

Post by Sander »

If you are using sessions, you can store the user id in the $_SESSION array after he logged in.

Based off your login function:

Code: Select all

function login($user, $pass) 
{
   // connect
   $conn = db_connect();
   if (!$conn)
      return false;
   
   // search for the user, ** note the 'select user_id' **
   $result = mysql_query("select user_id from user
                           where user = '$user'
                           and pass = MD5('$pass')");
  
   // found a user?
   if (!result)
   {
      return false;
   }
   if (mysql_num_rows($result)>0)
   {
      // store the user id
      $_SESSION['user_id'] = mysql_result($conn, 0, 'user_id');
      return true;
   }
   else
   {
      return false;
   }
}
User avatar
Devnull
Forum Commoner
Posts: 52
Joined: Fri Oct 22, 2004 2:19 pm

Post by Devnull »

Thanks a lot, so if I wanted to echo the user ID, I would use the following code right?

Code: Select all

<?php
   echo $_SESSION['user_id'];
?>
I would also have to re-write my get_area() function so when it extracts the user's area, that user's are matches the user_id. So my new function would be the following, right?

Code: Select all

function get_area() {
   $conn = db_connect();
   if (!$conn)
      return false;
    
   $result = mysql_query("select * from user
   			   where $_SESSION['user_id'] = id // THIS IS THE LINE I'M HAVING PROBLEMS WITH, I NEED TO ADD A STATEMENT THAT MATCHES THE SESSION ID TO THE USER ID!
                           and where area = '$area'");

// added a $ before result here
   if (!$result)
      return false;
   if (mysql_num_rows($result)>0)
      return true;
   else
      return false;

// here's a line to extract the row from the db
$array = mysql_fetch_assoc($result);

// store the province in $province
$uarea = $array['area'];
}
Last edited by Devnull on Sun Aug 07, 2005 2:52 pm, edited 1 time in total.
User avatar
Devnull
Forum Commoner
Posts: 52
Joined: Fri Oct 22, 2004 2:19 pm

Post by Devnull »

Anyone ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

please don't bump a thread within 24 hours of the last post...
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Devnull wrote: select * from user
where $_SESSION['user_id'] = id // THIS IS THE LINE I'M HAVING PROBLEMS WITH, I NEED TO ADD A STATEMENT THAT MATCHES THE SESSION ID TO THE USER ID!
and where area = '$area'")
Probably because $_SESSION['user_id'] is not a column in your database.



EDIT: here, I'll write it out for you:

Code: Select all

mysql_query("SELECT * FROM `user` WHERE `id` = " . (int)$_SESSION['id'] . " AND `area` = '$area' LIMIT 1");
You only need one WHERE clause, you can append additional conditionals by using the keyword 'AND', 'AND WHERE' is invalid. Also you should replace the asterisk (*) with the column names you want to extract, in case your table structure changes. Always append LIMIT 1 to a query where you only want 1 result, if for some reason another part of your script fails, and creates 2 users with the same id your script could perform unexpectedly if 2 rows are returned.
User avatar
Devnull
Forum Commoner
Posts: 52
Joined: Fri Oct 22, 2004 2:19 pm

Post by Devnull »

Thanks a lot!
Post Reply