Jump to content
xisto Community
coldasice

[mysql]get Id Of Loged In User?

Recommended Posts

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...

 

$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:

 

$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.

Share this post


Link to post
Share on other sites

thanks for your replay :)

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

any ways this is what i try :P

$username = $_SESSION["username"];

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

$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

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


Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 :)

 

 

ah.. i now run that code.. i still dont gett the user id.. :D

 

its blank

 

hmm....

 

 

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

 

is not valid.. :D

 

 

 

or maby the mysql code?

 

any one have a clue?

 

 

edit2:

 

okey.. my bad forgot that varibles was case sensitive.. :D

 

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

 

thanks thanks thanks.. and you to truefusion.. u get a hug :)

 

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

 

[sOLVED]

Edited by coldasice (see edit history)

Share this post


Link to post
Share on other sites

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:

$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
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 :D



**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 :P I can quote the whole thing if required.
Edited by shadowx (see edit history)

Share this post


Link to post
Share on other sites

in that case to sumize :D


my database is

id
username
password

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

after you have logged in :P

userid.php
-------------

<?phpsession_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 :D

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

// Display's the id and the user name :D
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.. :)
Edited by coldasice (see edit history)

Share this post


Link to post
Share on other sites

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 :P I'm glad you solved the problem and made something useful out of it

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.