Author Topic: MYSQL Query  (Read 1209 times)

Offline Emicarn

  • Level 5
  • *
  • Posts: 19
  • Reputation: +0/-0
    • View Profile
MYSQL Query
« on: December 23, 2010, 08:12:28 PM »
I'm able to write a new user to the database flawlessly.  It works and all data goes in.

When I'm trying to query for a log in routine it's not finding the user.

The query is written as:

Code: [Select]
$sqlfind = sprintf("select * from players where username = '$playername' and (password) = ('%s');", mysql_real_escape_string(md5('$playerpassword')));


It is returning no data found but the database clearly shows the user.  When I query the password as MD5 in phpMyAdmin it shows a matching string.

What am I overlooking here? 

I've tried the same query in phpMyAdmin and it returns zero rows.

Thanks!

~ Rich

http://www.sectorbattles.com [not ready for prime time]

Offline Sagefire135

  • Level 14
  • *
  • Posts: 107
  • Reputation: +2/-0
    • View Profile
Re: MYSQL Query
« Reply #1 on: December 23, 2010, 10:57:11 PM »
I THINK the problem is that you are trying to put a variable inside without using the %s thing. the query is literally looking for someone with the username $playername. do the same thing you did with the password and it should work.


Code: [Select]
$sqlfind = sprintf("select * from players where username = '%s' and password = '%s'", $playername, mysql_real_escape_string(md5('$playerpassword')));


Offline pixlepix

  • Level 12
  • *
  • Posts: 90
  • Reputation: +0/-0
    • View Profile
Re: MYSQL Query
« Reply #2 on: December 24, 2010, 07:01:09 AM »
I THINK the problem is that you are trying to put a variable inside without using the %s thing. the query is literally looking for someone with the username $playername. do the same thing you did with the password and it should work.


Code: [Select]
$sqlfind = sprintf("select * from players where username = '%s' and password = '%s'", $playername, mysql_real_escape_string(md5('$playerpassword')));


No, that isnt the problem. Or my whole game wouldnt work. Can we see the query putting the data into the players table?

Offline Nox

  • Level 35
  • **
  • Posts: 768
  • Reputation: +12/-2
    • View Profile
Re: MYSQL Query
« Reply #3 on: December 24, 2010, 07:13:49 AM »
@emicarn
1)
Not sure it's the reason, but you have swapped the target of escaping.

You do not need to escape password if you apply md5 to it first since afaik md5 contains only query-safe characters
But if the $playername is originated from user input, it needs to be escaped

2) MD5 is not considered safe method anymore, plus the most common case of rainbow tables, today it's usually some sha (sha1 is not ideal imho anymore too, I use the extreme variant sha512) + salt

----

Did you try to print the $sqlfind variable? Try echo $sqlfind and use it in phpMyAdmin, if it works, the problem is somewhere else (and we'd need to see more of the code)
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline Emicarn

  • Level 5
  • *
  • Posts: 19
  • Reputation: +0/-0
    • View Profile
Re: MYSQL Query
« Reply #4 on: December 24, 2010, 09:29:39 AM »
@emicarn
 
Did you try to print the $sqlfind variable? Try echo $sqlfind and use it in phpMyAdmin, if it works, the problem is somewhere else (and we'd need to see more of the code)

Tried this and fed it into phpMyAdmin  - returned zero rows.


Thanks!

~ Rich

http://www.sectorbattles.com [not ready for prime time]

Offline Emicarn

  • Level 5
  • *
  • Posts: 19
  • Reputation: +0/-0
    • View Profile
Re: MYSQL Query
« Reply #5 on: December 24, 2010, 09:30:32 AM »
I THINK the problem is that you are trying to put a variable inside without using the %s thing. the query is literally looking for someone with the username $playername. do the same thing you did with the password and it should work.


Code: [Select]
$sqlfind = sprintf("select * from players where username = '%s' and password = '%s'", $playername, mysql_real_escape_string(md5('$playerpassword')));


No, that isnt the problem. Or my whole game wouldnt work. Can we see the query putting the data into the players table?


The query to add users:

Code: [Select]

$adduser = sprintf("INSERT INTO players (username, password, email, shipname, faction, pos_x, pos_y, class, beta_tester, admin, confirmed, vessel_val, points, v_points, c_hull, c_shield, c_engine, c_battery, c_aux_power, c_sensors, c_beam_weapon, c_torpedo_weapon, c_dc, c_transporters, c_crew, c_marines, c_torpedos, m_hull, m_shield, m_engine, m_battery, m_aux_power, m_sensors, m_beam_weapon, m_torpedo_weapon, m_dc, m_transporters, m_crew, m_marines, m_torpedos, join_date, join_time, login_date, login_time) values ('%s', '%s', '%s', '%s', '%s', '$posX', '$posY', 'Gun Boat', '$isbeta_tester', '$isadmin', '$isconfirmed', '47', '0', '0', '10', '10', '10', '5', '5', '1', '2', '1', '5', '1', '10', '5', '10','10', '10', '10', '5', '5', '1', '2', '1', '5', '1', '10', '5', '10', '$date', '$time', '$date', '$time');", mysql_real_escape_string($playername), mysql_real_escape_string(md5($playerpassword)), mysql_real_escape_string($playeremail), mysql_real_escape_string($playershipname), mysql_real_escape_string($playerfaction));

Thanks!

~ Rich

http://www.sectorbattles.com [not ready for prime time]

Offline JGadrow

  • Level 35
  • **
  • Posts: 1,133
  • Reputation: +23/-2
    • View Profile
Re: MYSQL Query
« Reply #6 on: December 24, 2010, 09:46:46 AM »
The issue here is that you have single quotes around the $playerpassword variable inside of your md5() call. Thus, you're ALWAYS getting the md5 hash of the string '$playerpassword' instead of the user-entered password that you're expecting.

Try the following instead (minor re-write):

Code: (php) [Select]
$queryPattern = "SELECT * FROM players WHERE username = '%s' AND password = '%s';";
$sqlfind = sprintf($queryPattern, mysql_real_escape_string($playername), md5($playerpassword));

As a note, you can also use && in place of AND in the query. I prefer using && to keep it consistent with other programming languages' logical evaluation operators. But that's just personal preference. ;)

*Edit - Wrote query inside of a variable to prevent code scrolling in my forum theme.
« Last Edit: December 24, 2010, 09:48:57 AM by JGadrow »
Idiocy - Never underestimate the power of stupid people in large groups.


Offline Emicarn

  • Level 5
  • *
  • Posts: 19
  • Reputation: +0/-0
    • View Profile
Re: MYSQL Query
« Reply #7 on: December 24, 2010, 10:18:32 AM »
The issue here is that you have single quotes around the $playerpassword variable inside of your md5() call. Thus, you're ALWAYS getting the md5 hash of the string '$playerpassword' instead of the user-entered password that you're expecting.

Try the following instead (minor re-write):

Code: (php) [Select]
$queryPattern = "SELECT * FROM players WHERE username = '%s' AND password = '%s';";
$sqlfind = sprintf($queryPattern, mysql_real_escape_string($playername), md5($playerpassword));

As a note, you can also use && in place of AND in the query. I prefer using && to keep it consistent with other programming languages' logical evaluation operators. But that's just personal preference. ;)

*Edit - Wrote query inside of a variable to prevent code scrolling in my forum theme.

Good point on the &&. 

I dumped this in and tried it.  No rows returned.  It returned an echo string the exact same as the one I tried earlier from the original code.

Even cleared the players table and readded a new user.  Same result thinking maybe I forgot the test password [I'm old!].   

Thanks!

~ Rich

http://www.sectorbattles.com [not ready for prime time]

Offline Emicarn

  • Level 5
  • *
  • Posts: 19
  • Reputation: +0/-0
    • View Profile
Re: MYSQL Query
« Reply #8 on: December 24, 2010, 10:27:41 AM »
Quick Side Note:

I took out the md5 hashing.

It now works and returns a user.  The login works and can log in and out as different test users.
Thanks!

~ Rich

http://www.sectorbattles.com [not ready for prime time]

Offline JGadrow

  • Level 35
  • **
  • Posts: 1,133
  • Reputation: +23/-2
    • View Profile
Re: MYSQL Query
« Reply #9 on: December 24, 2010, 10:48:53 AM »
Did you remove the hashing only for the select query? Or for the insert as well? Perhaps you'd already created an md5 hash of the password earlier in your code?

Essentially, were you hashing an already hashed password (this will generate a different hash)?
Idiocy - Never underestimate the power of stupid people in large groups.


Offline Emicarn

  • Level 5
  • *
  • Posts: 19
  • Reputation: +0/-0
    • View Profile
Re: MYSQL Query
« Reply #10 on: December 24, 2010, 10:58:34 AM »
Did you remove the hashing only for the select query? Or for the insert as well? Perhaps you'd already created an md5 hash of the password earlier in your code?

Essentially, were you hashing an already hashed password (this will generate a different hash)?

I removed on the insert and then on the log in.

That way there is a plain text password in the DB.   

when I add back the md5 hash, and created a new user, it failed.

Thanks!

~ Rich

http://www.sectorbattles.com [not ready for prime time]

Offline Nox

  • Level 35
  • **
  • Posts: 768
  • Reputation: +12/-2
    • View Profile
Re: MYSQL Query
« Reply #11 on: December 24, 2010, 01:24:54 PM »
Did you try to echo and compare the md5's?

Do you have sufficient column length? MD5 needs 32chars, if the limit is shorter, it gets cropped and therefore do not match
« Last Edit: December 24, 2010, 01:45:59 PM by Nox »
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline Emicarn

  • Level 5
  • *
  • Posts: 19
  • Reputation: +0/-0
    • View Profile
Re: MYSQL Query
« Reply #12 on: December 24, 2010, 03:40:13 PM »
Did you try to echo and compare the md5's?

Do you have sufficient column length? MD5 needs 32chars, if the limit is shorter, it gets cropped and therefore do not match

This might be the problem.  They seem to match when I echo and compare.

I have the password field set to varchar(25)  Let me adjust that to a bigger field and see what happens.

This didn't cross my mind.........

Thanks!

~ Rich

http://www.sectorbattles.com [not ready for prime time]

Offline JGadrow

  • Level 35
  • **
  • Posts: 1,133
  • Reputation: +23/-2
    • View Profile
Re: MYSQL Query
« Reply #13 on: December 24, 2010, 04:19:45 PM »
I have the password field set to varchar(25)  Let me adjust that to a bigger field and see what happens.

Ah, there's the culprit! :) As a tip, you can declare this column as char(32) instead of varchar(32) since an md5 hash is always 32 characters long. Not much of an optimization, but it does save 1 byte or 2 per player since it's no longer necessary to store the length of the string.
Idiocy - Never underestimate the power of stupid people in large groups.


Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: MYSQL Query
« Reply #14 on: December 25, 2010, 03:26:40 AM »
I have the password field set to varchar(25)  Let me adjust that to a bigger field and see what happens.

Ah, there's the culprit! :) As a tip, you can declare this column as char(32) instead of varchar(32) since an md5 hash is always 32 characters long. Not much of an optimization, but it does save 1 byte or 2 per player since it's no longer necessary to store the length of the string.
It might be a big optimization if MyISAM + all other fields in the table are already fixed size (no varchar, text, blob), since fining a new row would be a simple multiplication of size*rownumber for MySQL engine.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal