I have a line in MySQL with information like the username and email address and I would like to find the id of the row based on the username field. Any ideas on how to find the id based on the username field?
SELECT id FROM users WHERE username = 'NStorm';
It is very basic sql:
select id from your_table where username = 'foo'
Related
I am wondering if it is possible to insert from another table (which I have managed to do) whilst also inserting a VALUE of a variable from the current php file?
I am aiming to get the user ID from another table, which I have gotten from selecting the email from the user input. I then need to insert a hash which is automatically created via a variable.
This is my current code that gets the correct id from the users table.
$forgot = $pdo->prepare("
INSERT INTO
forgot (
user_id
) SELECT
id
FROM
users
WHERE
email = :email
");
Now I just need to insert the VALUE of :hash too.
Would this need to be done with a separate query?
Thanks.
Try the following:
INSERT INTO
forgot (
user_id, hash
) SELECT
id, :hash
FROM
users
WHERE
email = :email
I have been working on a website where I am storing user data like, userid, username, password, email, rank, fname, and lname. I have created a registration page that inserts this data and a login page that requires your username and password. This creates a session and in the session with a username variable. How do I use the username to pull from the sql to find, for example, their email? I have tried looking this up but have found nothing. If someone would not mind linking me to a tutorial or something that would be amazing.
Thank you very much,
John Finberg
First search wether username is exists in the database table or not with below query.
select count(*) from Employee where UserName=#username.
if username is already available in the table then get the EmailId by using below query.
select UserName, EmailId from Employee where UserName=#username
Create a stored procedure that will take the username as parameter and return user's email address:
CREATE PROCEDURE [dbo].[uspGetUserRole] #userName VARCHAR(255)
,#emailaddress NVARCHAR(255) = '' OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
BEGIN TRY
SELECT #emailaddress = u.emailaddress
FROM [User] u
WHERE u.UserName = #userName
END TRY
BEGIN CATCH
THROW
END CATCH
END
You can then call this stored procedure to get the email address for the username that you pass:
DECLARE #emailaddress NVARCHAR(255)
[dbo].[uspGetUserRole] 'domain\username1', #emailaddress OUTPUT
SELECT #emailaddress
1.User can't login with there user name because username is may be a same but use of user id or user email that must be a unique.
2.on login retrieve a user data by user id or email stored there user name and user id in session.
3.now you can retrieve a user data by user id.
I have a MySQL table with id, username, datetime and entry_id.
I'm logging access to each entry. But I need just one row with same username and entry_id, because I use it just for notifications. How could I programatically delete useless rows? (using PHP and/or SQL)
EDIT:
username is normal string,
ON DUPLICATE KEY wasn't working, because I want to check for duplicity just combination of same username and entry_id. I need just one row, with specific entry_id and username. I want to update just datetime. It's hard to explain :)
So, on each visit is insered new row. I just want datetime to update in every unique username and entry_id combination and not to create always new row.
Thank you for advance!
Just check for a previous entry in the database. If there is one, update it, if there is not an entry - insert one.
After reading your edit it sounds like you want something like:
SELECT username, entry_id FROM <table name> WHERE username = <username> AND entry_id = <entry_id>;
If you want to update the datetime column for that row:
UPDATE <table> SET datetime = <yourdatetime> WHERE username = <username> AND entry_id = <entry_id>
i would like to ask you about my database query
i have this table admin_table in my data base
tag id name password profession
xxxxxx Jhon Begly 123 admin
i build a php page to insert a user name and password
i need to check the user name and password that i entered with its profession if its valid then proceed with a new page as bellow
$sql = "SELECT * FROM admin_table WHERE name='".$username."' AND password='".$password."'";
so how can i check profession in the same query above
any update will be highly appreciated
You mean this?
$sql = "SELECT * FROM admin_table WHERE name='".$username."'
AND password='".$password."' AND profession = 'admin'";
If you get a result row back, you matched all three columns. If zero rows, it failed to match at least one. Not really sure your use case.
I also agree with other commenters that this is far from best practices in several ways.
Where are you validating the profession? If it is in another table... use
INNER JOIN profession_table as pt
ON pt.some_column
right after the from part of your query
I have a database from which I would like users to retrieve information from a certain table called "entry" based on their username.
So I want user1 to login and then a select statement be created to take the username, look it up in the username table, and then used the stored value for the person's name (which is one of the columns in the user table) to run a query to show all records for that person.
Is this possible?
thanks in advance
EDIT:
Sorry the question was so badly formed.
Basically I have a users table which holds user login details with the fields - studentName, Site, Username , Password
I then have another table called entry which holds records for all users with the following fields - entryID, date, studentName , field1 , field2, field3 etc
What want to do is for the user to login as they do now and a query to be generated based on the Username to get all records for that particular student.
Does this give a better idea of what I am try to get?
if its Oracle, then you can do:
SELECT a,b,c
FROM entry
WHERE username = user -- user is an Oracle keyword that returns
-- the oracle user name of the user running the query.
Possibly way off but asuming a table called User with an Id, Name & UserName and a related table called details with a UserId and other columns with the details the below simple join will work. Also the string 'AUserName' is the value passed in from your app.
SELECT User.Name, Details.*
FROM User
INNER JOIN Details
ON User.Id = Details.UserId
WHERE User.Username = 'AUserName'
But this is a guess based on your question, add more details and you'll get a better answer