Nov 21, 2009

[mysql]get Id Of Loged In User?

free web hosting
Open Discussion > MODERATED AREA > Computers > Programming Languages > PHP Programming

[mysql]get Id Of Loged In User?

coldasice
how to get the id number of the loged in user?


my db is

id.
username.
password.

i have tryed a few things.. but i never seem to get it right ohmy.gif

Comment/Reply (w/o sign-up)

shadowx
Hey there, found the topic!

Im not sure how experienced you are with php and SQL so ive got this link here http://www.tizag.com/mysqlTutorial/mysqlselect.php its a good tutorial on the SELECT query in sql and the next page is using the WHERE clause within the SELECT query.


Now onto specific help, i've notched up this little code that should work when customized...

QUOTE
$query = mysql_query("SELECT * FROM Table_Name WHERE username='$username'", $link);

$row = mysql_fetch_array($query);

$ID = $row['id'];


Again as im not sure how much coding you've done ill explain it, if you know whats going on then just fill in the blanks and give it a whirl...

the first line is where the query is done, all you really have to do hee is change "Table_Name" to your table name and "$username" To whatever your username variable is called, EG when the user logs in you presumably store their username in a variable, this is the variable you want to use here (afte you've cleaned out bad code etc...)

The second line is where we transform the query result into an array, so now each field in the database is in this array eg:

QUOTE

$row['id']$row['username']
$row['password']


and the third line shows how you access those arrays and turn them into variables, does that help? Im sorry if this seems patronizing but i dont know how good you are with php so id rather cover it all than not enough.

 

 

 


Comment/Reply (w/o sign-up)

coldasice
thanks for your replay wink.gif

vell i have a basic understandig of php.. and not soo good at mysql.. but im readin wink.gif

any ways this is what i try wink.gif

QUOTE


$username = $_SESSION["username"];

$query = mysql_query("SELECT * FROM users WHERE username='$username'", $link); // your code wink.gif

$row = mysql_fetch_array($query);

$ID = $row['id'];


echo "<p>User ID:". $id;
echo "<p>Username: " . $_SESSION["username"];


but im not sure if that username = seesion work.. -.- if its leagal.. or somthing like that.. ;O

but i have to try ^^

but what does $link do?...


but i get error =D

QUOTE
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\Programfiler\wamp\www\login2\member.php on line 17

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Programfiler\wamp\www\login2\member.php on line 19

User ID:

Username:coldasice

Comment/Reply (w/o sign-up)

truefusion
QUOTE(coldasice @ Dec 19 2007, 11:54 AM) *
but what does $link do?

The reason you received the error is because there was no specified resource. The link variable, in this case, is what would hold that information. Such information is usually stored in a db.php file; the file that contains your database connection information.

Comment/Reply (w/o sign-up)

coldasice
QUOTE(truefusion @ Dec 19 2007, 06:36 PM) *
The reason you received the error is because there was no specified resource. The link variable, in this case, is what would hold that information. Such information is usually stored in a db.php file; the file that contains your database connection information.



ah.. thanks ;O

i alreay have a db file included wink.gif


ah.. i now run that code.. i still dont gett the user id.. sad.gif

its blank

hmm....


so my guess is that $username =$_session["username"] edit: this works ;O

is not valid.. sad.gif



or maby the mysql code?

any one have a clue?


edit2:

okey.. my bad forgot that varibles was case sensitive.. wink.gif

thanks alot this works just grate.. i hope this will help me further on my project wink.gif

thanks thanks thanks.. and you to truefusion.. u get a hug wink.gif

if you admins/mods do u can close and change the topic to solved.. if u want wink.gif

[SOLVED]

Comment/Reply (w/o sign-up)

shadowx
QUOTE
The reason you received the error is because there was no specified resource. The link variable, in this case, is what would hold that information
Exactly right, when you do anything between php and MYSQL you need to first connect to the database, as on that Tizag website at the start of the tutorial this is how to connect to a DB:

QUOTE


$link = mysql_connect("SERVER NAME", "USERNAME", "PASSWORD") or die(mysql_error());

mysql_select_db("DB NAME") or die(mysql_error());


The first line there starts by declaring the $link variable, you can call this variable whatever you want, then the value of that variable is not a string of characters its a connection and you make that connection with the MYSQL_CONNECT statement, note the parameters inside the brackets there, the first is the server name, now usually that is simply "localhost" unless your host tells you otherwise always use "localhost" there, the next one is username, your host will always tell you this, here at T17 i think it's the same as your hosting login name, which isnt the same as your forum username, the username im talking about is the one you use to login to CPANEL with, i could be wrong here, its been a long time since i setup my databases! For local use, for example if you have XAMPP or another local testing system with PHP and MYSQL your username will usually be "root" unless your installation tells you differently. And the last parameter is the password, again your host usually tells you this and with T17 i think its the password you use to login to CPANEL with and on a local installation you either set it up or it can be blank sometimes but check with the documentation of your installation if you have one.

Now if Im wrong about the T17 login stuff then this should be the right way to get a username and password... you login to CPANEL and go to the databases or MYSQL section, i cant remember what its called but its NOT PHPMyAdmin thats for managing the databases. So once into the MYSQL DATABASES section (or whatever its called) you will see any and all databases you have made, if you have'nt made one yet then you need to make one here now but from what you said you have made one ( i realise this info is specific to T17 so if you're not with T17 you may as well skip this) So here you find the DB you want, or make one, and then you can assign users to it, if i remember rightly they are in a drop down list, just select the user you want, if you dont see any users or you want to make a new one i think there is a button or link to make a new user, give it all privileges (note this can be insecure so do a google on MYSQL privileges if you want to make sure) And give this user a username and password and assign it to the DB, the username and password you made here will be used in the above line.

(END of T17 specific info so pickup from here if you skipped the above)

The second line in the code selects the DB to connect to, so far you have connected to the SERVER but not to any DATABASES on that server, It's fairly simple, just replace the "DB NAME" with the database name, this is NOT the TABLE name, for example if you're using PHPMyAdmin (AKA "PMA") then on the left is where you have a drop down list to select the database you want to see, EG you might have a DB called "LOGIN" with tables "Users" "admins" etc..... under it so the name you want is "LOGIN" as that is the DB name.

As you might know the
CODE
or die(mysql_error());

code at the end simply means that if there is an error it will give a nice error message rather than being silent and just not working.

does that make sense? Basically you just need to copy and paste this code ABOVE the rest of the database related code (most of us stick it right at the top of the page or just save those two lines into a separate file and use PHP INCLUDE to get the code) and change the bits I've talked about and it should work smile.gif



**EDIT I took a long time writing this and thats why I still posted it, i wouldnt have if i had seen the problem was now solved. Its up to the mods whether or not this stays here, i think it could be beneficial to other users but their decision is final as always tongue.gif I can quote the whole thing if required.

Comment/Reply (w/o sign-up)

coldasice
in that case to sumize wink.gif


my database is

id
username
password

this is how to get id from db yo the user that have loged in (session) wink.gif works if you have a session login scritp.. if not all is.. dunno ;O

after you have logged in wink.gif

userid.php
-------------
QUOTE
<?php
session_start();
include "database.php";

$username = $_SESSION["username"]; //makes sure your username is the loged in user name

$query = mysql_query("SELECT * FROM users WHERE username='$username'"); // basicly selects your id from given username..

$row = mysql_fetch_array($query); //basicly fetches the array lol ohmy.gif

$ID = $row['id']; //we all know what a variable is=?

// Display's the id and the user name wink.gif
echo "<p>User ID:". $ID;
echo "<p>Username: " . $_SESSION["username"];

?>


just tried to make it easy ;O

did i do a good job =D?

_________
shadowx

thanks alot for your secound in put.. to bad you didnt see this the last post i had.. wink.gif

Comment/Reply (w/o sign-up)

shadowx
Yup, that's how I'd do it... although im the neatest of coders it tends to work! One thing to remember is to clear all user input of suspicious characters like quotes etc... I made a login system that worked in a similar way to this as it goes, i used the user-entered username and password to query the DB and look for their ID etc.. and double check it that way, worked well. If you do turn this into a full login (or other useful) system then you should write it out as a tutorial so others can learn and you'll also get plenty of credits wink.gif

I'm glad you solved the problem and made something useful out of it

Comment/Reply (w/o sign-up)



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : , mysql, id, loged, user,

  1. Php Counting Row By Specific User And Limit!
    (6)
  2. Unexpected T_string In User.php [resolved]
    (5)
    Ok so i'm working on a site for my chruch, and i seem to be having a little bit of trouble with
    the user.php file i keep getting the following error: CODE Parse error: syntax error,
    unexpected T_STRING in /home/darkzone/public_html/test/user.php on line 195 and i was wondering
    if some one could help me with the script. session_start(); ob_start(); //Include the
    configurations include('config.php'); //Define a few variables $x = $_GET ; $u = $_GET
    ; class register { function displayform($title) { echo(' '.$title.' Use....
  3. Php Ftp Upload Form
    Adding User Directory to PHP Upload Form - Help (2)
    Alright I am trying to have a PHP FTP Upload Form that allows the user to create the directory
    folder for where they want to upload there files to. example: Main Directory: vainsoft.com There
    directory: vainsoft.com/modeling or vainsoft.com/photography But I dont want them to be able to
    upload things into the main directory, only sub-directories, is that possible with this coding that
    I have: CODE //uses $_FILES global array //see manual for older PHP version info //This
    function will be used to get the extension from the filename function get_extension($file,$....
  4. Compare 2 List Of User Ids From Different Tables
    (1)
    Hi all. I am trying to make a list out of 2 list. The first list is a complete list of users id
    from users table (List A) The Second list is of users id from another table (List /cool.gif"
    style="vertical-align:middle" emoid="B)" border="0" alt="cool.gif" /> I want to subtrack the user
    ids in List B from List A and make List C. Thanks in advance for any help /smile.gif"
    style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> Update: I looked up a old
    sql book i had and found some query examples. I tried the EXCEPT statement in mysql but it dont wor....
  5. Multiple Drop Down Lists ?
    Multiple drop down lists to take user to new page (4)
    Hi everyone I was wondering if anyone could help. I want to create a page with multiple drop down
    lists and depending what the user selects decides the page they will be taken to. Sorry i havent
    explained this too well. Here is an example of what i want (link below) the user is also emailed a
    copy http://www.dermalogica.com/SpeedMappingOnl...US®ion=B I have searched the web and come
    close but nothing does it right I would be extremely greatful if anyone could help! Thanks ....
  6. Directing To A User To Specific Page First Time Only
    (3)
    I make a signup page. I want that when someone signups and login for first time..then he is directed
    to the page X automatically. but after that it is redirected to Page Y always... Its pretty simple
    thing to do but im not understanding how i can make the script to identify that a person has logged
    in for first time ... anyhelp would be so welcome. Thanks.....
  7. Script That Tracks The User Status
    how can I track on or offline users? (4)
    long explaination: hey, I'm building a user profile site right now. And, I kinda know how to
    make a online/offline detector, but not totally sure. I know I can make a mysql database to track
    them, but how does it entrer the information? I could easily put in a field where when they login it
    sets them to online, but if they don't sign out, and just exit the browser, how can I tell.
    short: I want someone to tell me how to make a online/offline status detector, like they have here
    on trap17. I'd be thrilled if you can post to this, thanks, arcticsnpr....
  8. Password Strength / User Availablity Scripts ?
    (2)
    many of u guys would already have noticed that now a days , on most of the websites , when some one
    sign in...as he puts his desired username in the textbox , the page shows that it is not available
    or it is available...without pushing any button etc.. and the second thing , when some one writes
    the password , on the same screen , it shows that password is weak or password is strong... i think
    it is done with php ... can some one give me a link to any page where i can access these scripts ?
    or some one can help me regarding this ?? Thanks for helping always.....
  9. Cleaning User Input With Regex
    (1)
    As anyone who works with user input knows, not everyone who submits information makes it look
    proper. One one of my web forms, I parse all the needed fields that I wish to be title cased; their
    name, address, city, etc. I use this to perform this action, which works nicely: PHP Code: CODE
    $string = ucwords(strtolower($string)); This fixes the input if the user types in all caps
    or all lowercase. There is a problem I have been noticing about those who enter a generation after
    their name, such as "Bill Warner III". The above function changes it to "Bill Warn....
  10. User Login System With Setcookies
    (13)
    a friend of mine is quite good at php and told me not to use sessions and to use setcookie im not
    sure how to use setcookie to make a user authentication system and was wondering if anyone here know
    a tutorial on how to do it....
  11. Create A Windows User Account
    (1)
    hi, i am making an online registration scheme for my website for some webspace. i have got every
    thing sorted out exsept for one thing. to acces the ftp service and upload files to there user space
    i need to create a user account on the server for that user. is there a way to create a user account
    on a windows operating system with php 5 installed. thanks, kvarnerexpress ....
  12. Php Login Script
    Removing the login field once the user logs in (18)
    Hey everyone. I have a login script that i know works. My question is that on my main page, it have
    a form to allow the user to log in. What i want it to do is that once the user logs in, the form
    disappears and the users data (aka username) is displayed where the form was. At the moment i cant
    get it to work. Below is my code. CODE      if ($logged_in == 1)    {  ?>          
    }//if     else     {  ?>     " method="post">         Username:            
    Password:                     }//else ?> What happens....
  13. User Log-in
    PHP question (2)
    Hello all, I am a PHP n00b. I have read the Login page post, which is over my head. I was hoping
    someone could explain a concept to me more in layman's terms. This is what I hope to do - I want
    to go to my url (www.xyzblah.com) and have it automatically prompt for either a username and
    password, or just a password. After the correct credentials are supplied, go to the regular
    index.html. Any help you could give me is appreciated. Thanks.....

    1. Looking for , mysql, id, loged, user,

Searching Video's for , mysql, id, loged, user,
See Also,
advertisement


[mysql]get Id Of Loged In User?

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com