PHP how to use $_Session to transfer a value stored in a username text box to a sql query

Hi
I have an Android app that allows the user to look up books.

The app has login page that queries a PHP script (hosted in wamp) that checks the username and password and if it exists takes you to the main page.
The main page runs a PHP script that contains a SQL script that returns a list users of the app in a listview . Upto now this works ok.

However what i want to do is just return all users but not the user that logged in. I have read that $_Session can be used as a global variable. So I could use $_Session to store the username and then run the query to get all users <> user thats logged in.
Can anyone give me an example or point me to where i can find out how to use $_Session variable for this use?

For example im on the right track using it like this?

users.usertable
FROM users
WHERE EXISTS (SELECT 0 FROM available A1, available A2
WHERE users.uid <>$_Session [username])") or die(mysql_error())
wilko1000Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
Hopefully, your table has a primary key.  What you need to do is, upon login, retrieve the user's Primary key.  Then use that primary key to select all the other users, fitering out the current user.

So, if you table was
users
=====
`id` INTEGER NOT NULL AUTO_INCREMENT,
`firstname` varchar(50),
`lastname` varchar(50),
`username` varchar(50),
`password` varchar(50),
PRIMARY KEY (id)

Upon login, save the id onto a session variable:
<?php
// you must call this before tampering with $_SESSION
session_start();
...
# Then your query will basically be
# SELECT id FROM users WHERE username='...' and password='...'
...
 
#if successful, you will have only one record.  Retrieve that record and save it onto $_SESSION['user_id'].

Open in new window


Then on your other page, (which is your subsequent query) you would need:
<?php
session_start();

# connect to db
...

#craft your sql that will exclude the user that is currently logged in.
$sql = sprintf("select id, username FROM users WHERE id <> %d", $_SESSION['user_id']);

# now execute the $sql query above
...

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
If you're new to PHP and want some good learning resources, this article can help.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

$_SESSION (case-sensitive) is a PHP superglobal variable.  Used properly it can provide "stateful" information that is carried by the server between requests.  To fully understand how it works, you must understand the nature of HTTP Client/Server protocols.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/A_11271-Understanding-Client-Server-Protocols-and-Web-Applications.html

Once you've "got" that, the PHP session is very easy to use correctly!
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11909-PHP-Sessions-Simpler-Than-You-May-Think.html

Session data can be used in SQL queries, but you may find yourself dealing with fiddly punctuation because of all the quote marks, and there are fairly serious security implications of using external data in a query string.  Recommend you start with the first article and build up your experience in PHP a little at a time.  Or if time does not permit, consider hiring a professional developer to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.