Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. MySQL
  4. Need Help with MySQL Query for Matching Items

Need Help with MySQL Query for Matching Items

Scheduled Pinned Locked Moved MySQL
databasequestionmysqlregexhelp
4 Posts 2 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jim Fell
    wrote on last edited by
    #1

    Hello. I'm having a little trouble getting this query to work:

    $userId = mysql_real_escape_string( $_SESSION['user_id'] );
    $userPassProvided = mysql_real_escape_string( $_POST['oldPassword'] );
    $query = "SELECT user_id, AES_DECRYPT( user_pass, '".$db_aes_key."' ) AS user_pass ";
    $query .= "FROM users_tbl WHERE MATCH( user_id, user_pass ) ";
    $query .= "AGAINST( '".$userId."', '".$userPassProvided."' IN BOOLEAN MODE ) LIMIT 1";
    $result = mysql_query( $query, $mysql_db );

    What I would like to do is query users_tbl for the record wherein user_id and user_pass are the same as $userId and $userPassProvided, respectively. Can someone please tell me what is wrong with my query? Thanks. :)

    L 1 Reply Last reply
    0
    • J Jim Fell

      Hello. I'm having a little trouble getting this query to work:

      $userId = mysql_real_escape_string( $_SESSION['user_id'] );
      $userPassProvided = mysql_real_escape_string( $_POST['oldPassword'] );
      $query = "SELECT user_id, AES_DECRYPT( user_pass, '".$db_aes_key."' ) AS user_pass ";
      $query .= "FROM users_tbl WHERE MATCH( user_id, user_pass ) ";
      $query .= "AGAINST( '".$userId."', '".$userPassProvided."' IN BOOLEAN MODE ) LIMIT 1";
      $result = mysql_query( $query, $mysql_db );

      What I would like to do is query users_tbl for the record wherein user_id and user_pass are the same as $userId and $userPassProvided, respectively. Can someone please tell me what is wrong with my query? Thanks. :)

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, not completely sure, but I think your SQL statement will search for $userPassProvided in field user_pass, and only when some row matches, will it return what you specified, including a decrypted password. If so, you should encrypt $userPassProvided. And I don't like two-way encryption for passwords; most often all you need is validating the user, which can be done with a one-way encryption (i.e. get password as entered, encrypt it, search it in the DB). BTW: I'm baffled by the MATCH...AGAINST in there too; you do want a full match, don't you? :)

      Luc Pattyn


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      J 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, not completely sure, but I think your SQL statement will search for $userPassProvided in field user_pass, and only when some row matches, will it return what you specified, including a decrypted password. If so, you should encrypt $userPassProvided. And I don't like two-way encryption for passwords; most often all you need is validating the user, which can be done with a one-way encryption (i.e. get password as entered, encrypt it, search it in the DB). BTW: I'm baffled by the MATCH...AGAINST in there too; you do want a full match, don't you? :)

        Luc Pattyn


        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


        J Offline
        J Offline
        Jim Fell
        wrote on last edited by
        #3

        Thanks, Luc. If I understand correctly, I should be doing something like this (untested of course):

        $userId = mysql_real_escape_string( $_SESSION['user_id'] );
        $userPassProvided = mysql_real_escape_string( $_POST['oldPassword'] );
        $query = "SELECT user_id, AES_DECRYPT(user_pass, '".$db_aes_key."' ) AS user_pass ";
        $query .= "FROM users_tbl WHERE MATCH( user_id, user_pass ) ";
        $query .= "AGAINST( '".$userId."', AES_ENCRYPT( '".$userPassProvided."', '".$db_aes_key."' ) IN BOOLEAN MODE ) LIMIT 1";
        $result = mysql_query( $query, $mysql_db );

        Yes, I do want an exact match. This is going to be used in a User Control Panel on my website when the user wants to change their password.

        L 1 Reply Last reply
        0
        • J Jim Fell

          Thanks, Luc. If I understand correctly, I should be doing something like this (untested of course):

          $userId = mysql_real_escape_string( $_SESSION['user_id'] );
          $userPassProvided = mysql_real_escape_string( $_POST['oldPassword'] );
          $query = "SELECT user_id, AES_DECRYPT(user_pass, '".$db_aes_key."' ) AS user_pass ";
          $query .= "FROM users_tbl WHERE MATCH( user_id, user_pass ) ";
          $query .= "AGAINST( '".$userId."', AES_ENCRYPT( '".$userPassProvided."', '".$db_aes_key."' ) IN BOOLEAN MODE ) LIMIT 1";
          $result = mysql_query( $query, $mysql_db );

          Yes, I do want an exact match. This is going to be used in a User Control Panel on my website when the user wants to change their password.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          for an exact match, you don't need MATCH...AGAINST, a simple "... WHERE field1 = 'value1' AND field2 = 'value2' ..." would do. And you don't need to SELECT the password, the user_id should be sufficient. After all, you either get zero or one rows that match. :)

          Luc Pattyn


          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups