Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,419 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,352 people online right now. Registration is fast and FREE... Join Now!




Personalizing the query

 
Reply to this topicStart new topic

Personalizing the query

triplexduplex
15 Oct, 2008 - 06:58 AM
Post #1

New D.I.C Head
*

Joined: 4 Apr, 2008
Posts: 19

Hi, I have a really simple question .. again tongue.gif

What must I enter when I want to show the specific user's information?
Say, i have logged in as 'King' (which is the username). By right, When the player first logs in he will get directed to a page that should say 'Hello King!'

However, right now it shows 'Hello Bot' instead, because Bot is the first user on my table.

This is my code right now if you want to see:
CODE

<?php
mysql_connect("localhost", "USER", "PASSWORD") or die(mysql_error());
mysql_select_db("DATABASE") or die(mysql_error());

$result = mysql_query("SELECT * FROM members")
or die(mysql_error());  

$row = mysql_fetch_array( $result );

echo "Hello ".$row['username'];
?>


So what can I do to make this query .. personalized? Such that if i log in as king it will show 'Hello King!' and if I log in as queen it will show 'Hello Queen!' etc.


Thanks in advance.

User is offlineProfile CardPM
+Quote Post

Trogdor
RE: Personalizing The Query
15 Oct, 2008 - 07:55 AM
Post #2

D.I.C Addict
Group Icon

Joined: 6 Oct, 2006
Posts: 523



Thanked: 3 times
Dream Kudos: 125
My Contributions
That is where the WHERE clause comes in.
The most important thing to grasp at this stage however is that every reccord in the table (even in the whole database) has a unique ID.

I assume the members table looks somewhat like this:
ID autonumber,
username varchar(20),
password varchar(20),
lastusedip varchar(16),
credits int,

etc.

Now, during 'logging in' this person has to enter some credentials.
Say a username and a password.
These are then checked against the values in the database.
For example as follows:
SELECT ID FROM members WHERE username='$username' AND password='$password'
If this query returns no rows, the credentials are not valid.
If it does return a row, the userid is the first field. You should store this in the users' session.


On all your next pages/clicks/whatever you do, you can re-collect all needed (and current!) data with something like: SELECT * FROM members WHERE ID='$userid'
Ofcourse you should first get the userid from the session and check if it is still containing a value. If not the user most probably had a session timeout, or someone that is not logged in is trying to access a page for logged-in members.


Hope this helps.
User is offlineProfile CardPM
+Quote Post

triplexduplex
RE: Personalizing The Query
18 Oct, 2008 - 06:36 AM
Post #3

New D.I.C Head
*

Joined: 4 Apr, 2008
Posts: 19

Hmm.
I somehwat understood what you said, so I played around with some stuff.
Right now, I can get the desired username to show. I tried to show the password and that works too. However, when i try to tell the user what their ID is it does not work.

For example ' Hello King, your user ID is 1.' and 'Hello Queen, your user ID is 2.' All this does not show all I get now is 'Hello King, your user ID is .' sad.gif


My Codes
CODE

$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$myuserid = $_POST['myuserid'];

$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' AND password='$mypassword'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count==1){
session_register("myusername");
session_register("mypassword");
session_register("myuserid");
header("location:loginsuccess.php");
}
else {
echo "Wrong Username or Password";
}



CODE

$result = mysql_query("SELECT * FROM members WHERE ID='$myuserid'")
or die(mysql_error());  

echo "Hello ".$myusername;
echo ". Your ID is ".$myuserid;



What am I doing wrong?

Would appreciate the help, thanks!

This post has been edited by triplexduplex: 18 Oct, 2008 - 10:09 PM
User is offlineProfile CardPM
+Quote Post

Trogdor
RE: Personalizing The Query
21 Oct, 2008 - 03:53 AM
Post #4

D.I.C Addict
Group Icon

Joined: 6 Oct, 2006
Posts: 523



Thanked: 3 times
Dream Kudos: 125
My Contributions
That is because you are not actually putting the userid in the session variable.

You have to do first your query, and then use the answer.
I would also, for debugging purposes, disable the redirection part and just print out the result on screen.

The sequence of events should be:
1 register session variables.
2 filter the input
3 run the query
4 check the query result
5 store the result in the session
6 redirect.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 01:04PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month