Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SELECT issue

Status
Not open for further replies.

paulbradley

Programmer
Oct 9, 2002
158
0
0
GB
I am making a login function where a user enters a username and password. This is looked up and then a result is displayed if they have the password correct. I have the following code:

query = "SELECT password FROM users WHERE username = $username";
$result = mysql_query($query);

if ($result == $password) {
echo "your signed in";
}
else
{
echo "oops! wrong password";
}

But it always outputs that the password was wrong when I know it was right - any ideas? Thanks in advance.
 
PS the query word in line 1 does have a '$' before it! Just didn't copy.
 
Is this PHP?

I'm not a PHP user, but as far as I know, the mysql_query function returns a result code indicating success or failure, but the result set must be accessed using mysql_fetch_array().
 
And the simplest test for bad sql? Print it out and paste it into the mysql shell!
 

Hi Guys.. I am trying to run this query with SQLyog with MYSQL. SQLyog is a GUI for Mysql connection. You can run SQL command inside the GUI.

I would like to select all information insert into the database from 1/1/2005 till now.

A friend of mine ask me to try..

select * from `testingTable`.`customers` where date_cr > to_date('20050101'YYYYMMDD')

But the command didn't work. Can someone tell me the correct format for this???

Thanks

Felix

 
You should re-post that as a new thread. We wouldn't want to divert attention from the original problem, would we?
 
I've put '' around the $username variable so my code now looks like this:

$result = mysql_query("SELECT password FROM users WHERE username = '$username'");

if ($result == $password) {
echo "your signed in";
}
else
{
echo "oops! wrong password";
}

but still always returning that the password is incorrect. Any more ideas? thanks
 
If you want to stick at all cost with mysql_query :
Code:
$result = mysql_query("SELECT * FROM users WHERE username = '$username' AND password='$password'");

if ($result) {
echo "your signed in";
}
else
{
echo "oops! wrong password";
}

Of course you should validate the username and password against sql injection before performing this query.

By the way, this thread should have been moved to PHP
--
PG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top