Hello everyone,
I'm having a little trouble here.
I'm trying to use a session created from log in(the user id)
and take that user id and pull out information from the data base.

An example would be:
(this is not the actual code, but I will wrap it anyway)

$_session['username'];
$userId = 'username';

$query ="SELECT * FROM databasetable where userId = (whatever I need to draw out)";
 ( **Here's where the confusion comes in.** How would I take the $userId variable from the users login and get information such as sex being male or female, and then print userId's of the opposite sex? **)

So far I have tried about a hundred different ways with no luck, now I turn to the experts.
So I have totally confused myself - I hope I am asking the question coherently

Recommended Answers

All 11 Replies

Use your where clause to filter out the data.

$gender = $_SESSION['gender']; // or how ever you are accessing the gender data
$query ="SELECT id FROM databasetable where gender <> '$gender' "; // Will select id's of the opposite sex

when user is login save userid in session from that point only.
so you can directly use it,
$query ="SELECT * FROM databasetable where userId = ".$_session;
$result = ...fetch above query....

$oppositeSql = "SELECT * FROM databasetable where gender != ".$result[0];
this will give opposite gender's list

I agree that vibhadevit's method would also work just a well but there are a few points to keep in mind when constructing your queries.

1. Data that belongs to a user, such as gender, and is used repeatedly throughout the site (in searches for example) is best kept in a session variable. Having to run a query just to retrieve the current user's gender to be used in yet another query only adds to the load on your SQL server (especially on busy sites).

2. Stay away from SELECT * whenever possible unless you are actually going to use ALL the information for that record. Again, it's needless wear and tear on the db. If you only need id's, only select id's. Primary auto-incrementing keys are indexed by default and thus speeds your query if the id is all you are after. The busier your site gets, the larger your db gets, and the larger the load on your db as well.

Thinking ahead and cutting your server load wherever possible will eliminate a lot of "performance anxiety" if your site every gets super-busy or handles large volumes of information.

- Good luck with your project! 8-)

I was curious about this one.
Would you have to do two separate queries to set the users id and the gender?
Based on what CFROG said, whats the best way to query both?
Thanks
Dan

Member Avatar for rajarajan2017

Yes you can.

select * from table where gender!=(select gender from symbols where user id=$userid);

When you say "set" do you mean to a session variable?

A good place to set your session vars is during the login process. You're already performing a query to lookup the users credentials ... if the user checks out just set the vars and send him on his way. You'll be able to access any of the users session vars anywhere on the site.

rajarajan07
what do you mean from symbols?

CFROG Yes, that's what I meant.
So when the user logs in and his user name and password match, how do I set the users password and gender?
The way I tried it did not work, it said something about not changing the header, but there were no spaces or anything before anything else.
meaning

$_SESSION['whatever'] = $whatever;

came at the very beginning of the page.

Sorry, I meant user id and gender.

Here is an example you can use as a reference:

$sql = "SELECT id, gender, and_whatever_else FROM users WHERE 
           $user_cond AND pwd = '$pwd' "; 

			
$result = mysql_query($sql) or die (mysql_error()); 
$num = mysql_num_rows($result);

    if ( $num > 0 ) { // set session vars and login user
	
	list($id, $gender, $whatever_else) = mysql_fetch_row($result);

	   session_start(); 

	   // this sets variables in the session 

		$_SESSION['user_id']= $id;  
		$_SESSION['gender'] = $gender;
                $_SESSION['whatever_else'] = $whatever_else; 
 } 
else { // if authentication fails
      // do something here }

That is just an example, it would have to be modified to use as an actual login but the basic concept is there.

HTH

<?php

$sql = mysql_query("SELECT user_id, gender FROM user_table WHERE user_id='".$user_id."'");
$row = mysql_fetch_array($sql);
$id = $row['user_id'];
$gender = $row['gender'];

$sql2 =  mysql_query("SELECT user_id FROM user_table WHERE user_id!='".$id."' AND gender!='".$gender."'");
while($row2 = mysql_fetch_array($sql2)){
echo $row2['id']."<br />"; // test result, echoed only user_id, since you only want the opposite sex's id
}

?>

i hope this helps :)

Member Avatar for rajarajan2017
select * from table where gender!=(select gender from table where user id=$userid);

Mistakenly include my table name.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.