CONCAT + TRIM no longer working after encryption











up vote
-2
down vote

favorite












Since encrypting certain information in my database I am having a great deal of difficulty using an existing search function. Its function is to search a table of students by either first, last or both names.



The old working code is as follows:



SELECT *
FROM students
WHERE CONCAT(TRIM(firstname), ' ', TRIM(lastname)) LIKE '%$result%'
AND currentyear = $currentyear
ORDER BY students.lastname


The new non working code following encryption:



SELECT *, 
AES_DECRYPT(firstname,UNHEX(SHA2('',512))) AS stfirst,
AES_DECRYPT(lastname,UNHEX(SHA2('',512))) AS stlast
FROM students
WHERE CONCAT(TRIM(stfirst), ' ', TRIM(stlast)) LIKE '%$result%'
AND currentyear = $currentyear
ORDER BY stlast


Am I missing something really stupid here? Thank you.










share|improve this question




















  • 1




    Please don't passwords and change them in your live instance now.
    – Lithilion
    Nov 7 at 9:04












  • Why did you encrypt the data in tables ? Queries used now will be very inefficient, and won't be able to use indexing
    – Madhur Bhaiya
    Nov 7 at 9:06










  • I had to encrypt the data for new European GDPR regulations.
    – Phil Howell
    Nov 7 at 9:08










  • Still, might make more sense to split your $result into two(?) parts instead, and encrypted those, so that you can compare them to the already encrypted column contents … (That might leave the issue of trimming, but why was that not applied when the data was inserted to begin with? If neglected at that point, maybe time to fire a decrypt-trim-encrypt update query now first of all.)
    – misorude
    Nov 7 at 9:25










  • Please tag with the database you are using.
    – Gordon Linoff
    Nov 7 at 12:25















up vote
-2
down vote

favorite












Since encrypting certain information in my database I am having a great deal of difficulty using an existing search function. Its function is to search a table of students by either first, last or both names.



The old working code is as follows:



SELECT *
FROM students
WHERE CONCAT(TRIM(firstname), ' ', TRIM(lastname)) LIKE '%$result%'
AND currentyear = $currentyear
ORDER BY students.lastname


The new non working code following encryption:



SELECT *, 
AES_DECRYPT(firstname,UNHEX(SHA2('',512))) AS stfirst,
AES_DECRYPT(lastname,UNHEX(SHA2('',512))) AS stlast
FROM students
WHERE CONCAT(TRIM(stfirst), ' ', TRIM(stlast)) LIKE '%$result%'
AND currentyear = $currentyear
ORDER BY stlast


Am I missing something really stupid here? Thank you.










share|improve this question




















  • 1




    Please don't passwords and change them in your live instance now.
    – Lithilion
    Nov 7 at 9:04












  • Why did you encrypt the data in tables ? Queries used now will be very inefficient, and won't be able to use indexing
    – Madhur Bhaiya
    Nov 7 at 9:06










  • I had to encrypt the data for new European GDPR regulations.
    – Phil Howell
    Nov 7 at 9:08










  • Still, might make more sense to split your $result into two(?) parts instead, and encrypted those, so that you can compare them to the already encrypted column contents … (That might leave the issue of trimming, but why was that not applied when the data was inserted to begin with? If neglected at that point, maybe time to fire a decrypt-trim-encrypt update query now first of all.)
    – misorude
    Nov 7 at 9:25










  • Please tag with the database you are using.
    – Gordon Linoff
    Nov 7 at 12:25













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











Since encrypting certain information in my database I am having a great deal of difficulty using an existing search function. Its function is to search a table of students by either first, last or both names.



The old working code is as follows:



SELECT *
FROM students
WHERE CONCAT(TRIM(firstname), ' ', TRIM(lastname)) LIKE '%$result%'
AND currentyear = $currentyear
ORDER BY students.lastname


The new non working code following encryption:



SELECT *, 
AES_DECRYPT(firstname,UNHEX(SHA2('',512))) AS stfirst,
AES_DECRYPT(lastname,UNHEX(SHA2('',512))) AS stlast
FROM students
WHERE CONCAT(TRIM(stfirst), ' ', TRIM(stlast)) LIKE '%$result%'
AND currentyear = $currentyear
ORDER BY stlast


Am I missing something really stupid here? Thank you.










share|improve this question















Since encrypting certain information in my database I am having a great deal of difficulty using an existing search function. Its function is to search a table of students by either first, last or both names.



The old working code is as follows:



SELECT *
FROM students
WHERE CONCAT(TRIM(firstname), ' ', TRIM(lastname)) LIKE '%$result%'
AND currentyear = $currentyear
ORDER BY students.lastname


The new non working code following encryption:



SELECT *, 
AES_DECRYPT(firstname,UNHEX(SHA2('',512))) AS stfirst,
AES_DECRYPT(lastname,UNHEX(SHA2('',512))) AS stlast
FROM students
WHERE CONCAT(TRIM(stfirst), ' ', TRIM(stlast)) LIKE '%$result%'
AND currentyear = $currentyear
ORDER BY stlast


Am I missing something really stupid here? Thank you.







php sql database concat trim






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 9:08

























asked Nov 7 at 9:01









Phil Howell

2418




2418








  • 1




    Please don't passwords and change them in your live instance now.
    – Lithilion
    Nov 7 at 9:04












  • Why did you encrypt the data in tables ? Queries used now will be very inefficient, and won't be able to use indexing
    – Madhur Bhaiya
    Nov 7 at 9:06










  • I had to encrypt the data for new European GDPR regulations.
    – Phil Howell
    Nov 7 at 9:08










  • Still, might make more sense to split your $result into two(?) parts instead, and encrypted those, so that you can compare them to the already encrypted column contents … (That might leave the issue of trimming, but why was that not applied when the data was inserted to begin with? If neglected at that point, maybe time to fire a decrypt-trim-encrypt update query now first of all.)
    – misorude
    Nov 7 at 9:25










  • Please tag with the database you are using.
    – Gordon Linoff
    Nov 7 at 12:25














  • 1




    Please don't passwords and change them in your live instance now.
    – Lithilion
    Nov 7 at 9:04












  • Why did you encrypt the data in tables ? Queries used now will be very inefficient, and won't be able to use indexing
    – Madhur Bhaiya
    Nov 7 at 9:06










  • I had to encrypt the data for new European GDPR regulations.
    – Phil Howell
    Nov 7 at 9:08










  • Still, might make more sense to split your $result into two(?) parts instead, and encrypted those, so that you can compare them to the already encrypted column contents … (That might leave the issue of trimming, but why was that not applied when the data was inserted to begin with? If neglected at that point, maybe time to fire a decrypt-trim-encrypt update query now first of all.)
    – misorude
    Nov 7 at 9:25










  • Please tag with the database you are using.
    – Gordon Linoff
    Nov 7 at 12:25








1




1




Please don't passwords and change them in your live instance now.
– Lithilion
Nov 7 at 9:04






Please don't passwords and change them in your live instance now.
– Lithilion
Nov 7 at 9:04














Why did you encrypt the data in tables ? Queries used now will be very inefficient, and won't be able to use indexing
– Madhur Bhaiya
Nov 7 at 9:06




Why did you encrypt the data in tables ? Queries used now will be very inefficient, and won't be able to use indexing
– Madhur Bhaiya
Nov 7 at 9:06












I had to encrypt the data for new European GDPR regulations.
– Phil Howell
Nov 7 at 9:08




I had to encrypt the data for new European GDPR regulations.
– Phil Howell
Nov 7 at 9:08












Still, might make more sense to split your $result into two(?) parts instead, and encrypted those, so that you can compare them to the already encrypted column contents … (That might leave the issue of trimming, but why was that not applied when the data was inserted to begin with? If neglected at that point, maybe time to fire a decrypt-trim-encrypt update query now first of all.)
– misorude
Nov 7 at 9:25




Still, might make more sense to split your $result into two(?) parts instead, and encrypted those, so that you can compare them to the already encrypted column contents … (That might leave the issue of trimming, but why was that not applied when the data was inserted to begin with? If neglected at that point, maybe time to fire a decrypt-trim-encrypt update query now first of all.)
– misorude
Nov 7 at 9:25












Please tag with the database you are using.
– Gordon Linoff
Nov 7 at 12:25




Please tag with the database you are using.
– Gordon Linoff
Nov 7 at 12:25












2 Answers
2






active

oldest

votes

















up vote
0
down vote













you can't use select alias name in where condition .. you should repeat the code



and if you want compare encrypted values you should also encypt the matching value



and last try using concat('%', $result,'%') instead of '%$result%'



SELECT *
, AES_DECRYPT(firstname,UNHEX(SHA2('',512))) AS stfirst
, AES_DECRYPT(lastname,UNHEX(SHA2('',512))) AS stlast
FROM students
WHERE CONCAT(TRIM(AES_DECRYPT(firstname,UNHEX(SHA2('',512)))), ' ',
TRIM(AES_DECRYPT(lastname,UNHEX(SHA2('',512)))))
LIKE TRIM(AES_DECRYPT( concat('%', $result,'%'),
UNHEX(SHA2('',512)))))
AND currentyear = $currentyear
ORDER BY stlast





share|improve this answer























  • I had tried this and have just done so again but that isn't working either I'm afraid.
    – Phil Howell
    Nov 7 at 9:06










  • what mean "is not working" ?... you have error ? show error message .. wrong result? show the expected result and you actual result
    – scaisEdge
    Nov 7 at 9:07










  • No error message, the code works but it is delivering 0 results. As I said earlier the issue has only stopped working since I have been using encrypted data. It appears to be an issue with the decryption.
    – Phil Howell
    Nov 7 at 9:10












  • Remove the WHERE clause, and see if the values you selected under the aliases stfirst and stlast contain properly “decoded” names as you expect …?
    – misorude
    Nov 7 at 9:20










  • @PhilHowell .. answer updated .. (anyway seems strange that the where work using alias)
    – scaisEdge
    Nov 7 at 9:23


















up vote
0
down vote













Presumably, you are using MySQL. If so, MySQL extends SQL so you can use a HAVING clause with column aliases, So, you can write the query as:



SELECT s.*, 
AES_DECRYPT(firstname,UNHEX(SHA2('', 512))) AS stfirst,
AES_DECRYPT(lastname,UNHEX(SHA2('', 512))) AS stlast
FROM students s
WHERE currentyear = $currentyear
HAVING CONCAT(TRIM(stfirst), ' ', TRIM(stlast)) LIKE '%$result%'
ORDER BY stlast;





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186239%2fconcat-trim-no-longer-working-after-encryption%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    you can't use select alias name in where condition .. you should repeat the code



    and if you want compare encrypted values you should also encypt the matching value



    and last try using concat('%', $result,'%') instead of '%$result%'



    SELECT *
    , AES_DECRYPT(firstname,UNHEX(SHA2('',512))) AS stfirst
    , AES_DECRYPT(lastname,UNHEX(SHA2('',512))) AS stlast
    FROM students
    WHERE CONCAT(TRIM(AES_DECRYPT(firstname,UNHEX(SHA2('',512)))), ' ',
    TRIM(AES_DECRYPT(lastname,UNHEX(SHA2('',512)))))
    LIKE TRIM(AES_DECRYPT( concat('%', $result,'%'),
    UNHEX(SHA2('',512)))))
    AND currentyear = $currentyear
    ORDER BY stlast





    share|improve this answer























    • I had tried this and have just done so again but that isn't working either I'm afraid.
      – Phil Howell
      Nov 7 at 9:06










    • what mean "is not working" ?... you have error ? show error message .. wrong result? show the expected result and you actual result
      – scaisEdge
      Nov 7 at 9:07










    • No error message, the code works but it is delivering 0 results. As I said earlier the issue has only stopped working since I have been using encrypted data. It appears to be an issue with the decryption.
      – Phil Howell
      Nov 7 at 9:10












    • Remove the WHERE clause, and see if the values you selected under the aliases stfirst and stlast contain properly “decoded” names as you expect …?
      – misorude
      Nov 7 at 9:20










    • @PhilHowell .. answer updated .. (anyway seems strange that the where work using alias)
      – scaisEdge
      Nov 7 at 9:23















    up vote
    0
    down vote













    you can't use select alias name in where condition .. you should repeat the code



    and if you want compare encrypted values you should also encypt the matching value



    and last try using concat('%', $result,'%') instead of '%$result%'



    SELECT *
    , AES_DECRYPT(firstname,UNHEX(SHA2('',512))) AS stfirst
    , AES_DECRYPT(lastname,UNHEX(SHA2('',512))) AS stlast
    FROM students
    WHERE CONCAT(TRIM(AES_DECRYPT(firstname,UNHEX(SHA2('',512)))), ' ',
    TRIM(AES_DECRYPT(lastname,UNHEX(SHA2('',512)))))
    LIKE TRIM(AES_DECRYPT( concat('%', $result,'%'),
    UNHEX(SHA2('',512)))))
    AND currentyear = $currentyear
    ORDER BY stlast





    share|improve this answer























    • I had tried this and have just done so again but that isn't working either I'm afraid.
      – Phil Howell
      Nov 7 at 9:06










    • what mean "is not working" ?... you have error ? show error message .. wrong result? show the expected result and you actual result
      – scaisEdge
      Nov 7 at 9:07










    • No error message, the code works but it is delivering 0 results. As I said earlier the issue has only stopped working since I have been using encrypted data. It appears to be an issue with the decryption.
      – Phil Howell
      Nov 7 at 9:10












    • Remove the WHERE clause, and see if the values you selected under the aliases stfirst and stlast contain properly “decoded” names as you expect …?
      – misorude
      Nov 7 at 9:20










    • @PhilHowell .. answer updated .. (anyway seems strange that the where work using alias)
      – scaisEdge
      Nov 7 at 9:23













    up vote
    0
    down vote










    up vote
    0
    down vote









    you can't use select alias name in where condition .. you should repeat the code



    and if you want compare encrypted values you should also encypt the matching value



    and last try using concat('%', $result,'%') instead of '%$result%'



    SELECT *
    , AES_DECRYPT(firstname,UNHEX(SHA2('',512))) AS stfirst
    , AES_DECRYPT(lastname,UNHEX(SHA2('',512))) AS stlast
    FROM students
    WHERE CONCAT(TRIM(AES_DECRYPT(firstname,UNHEX(SHA2('',512)))), ' ',
    TRIM(AES_DECRYPT(lastname,UNHEX(SHA2('',512)))))
    LIKE TRIM(AES_DECRYPT( concat('%', $result,'%'),
    UNHEX(SHA2('',512)))))
    AND currentyear = $currentyear
    ORDER BY stlast





    share|improve this answer














    you can't use select alias name in where condition .. you should repeat the code



    and if you want compare encrypted values you should also encypt the matching value



    and last try using concat('%', $result,'%') instead of '%$result%'



    SELECT *
    , AES_DECRYPT(firstname,UNHEX(SHA2('',512))) AS stfirst
    , AES_DECRYPT(lastname,UNHEX(SHA2('',512))) AS stlast
    FROM students
    WHERE CONCAT(TRIM(AES_DECRYPT(firstname,UNHEX(SHA2('',512)))), ' ',
    TRIM(AES_DECRYPT(lastname,UNHEX(SHA2('',512)))))
    LIKE TRIM(AES_DECRYPT( concat('%', $result,'%'),
    UNHEX(SHA2('',512)))))
    AND currentyear = $currentyear
    ORDER BY stlast






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 7 at 11:56









    Phil Howell

    2418




    2418










    answered Nov 7 at 9:04









    scaisEdge

    89.3k94668




    89.3k94668












    • I had tried this and have just done so again but that isn't working either I'm afraid.
      – Phil Howell
      Nov 7 at 9:06










    • what mean "is not working" ?... you have error ? show error message .. wrong result? show the expected result and you actual result
      – scaisEdge
      Nov 7 at 9:07










    • No error message, the code works but it is delivering 0 results. As I said earlier the issue has only stopped working since I have been using encrypted data. It appears to be an issue with the decryption.
      – Phil Howell
      Nov 7 at 9:10












    • Remove the WHERE clause, and see if the values you selected under the aliases stfirst and stlast contain properly “decoded” names as you expect …?
      – misorude
      Nov 7 at 9:20










    • @PhilHowell .. answer updated .. (anyway seems strange that the where work using alias)
      – scaisEdge
      Nov 7 at 9:23


















    • I had tried this and have just done so again but that isn't working either I'm afraid.
      – Phil Howell
      Nov 7 at 9:06










    • what mean "is not working" ?... you have error ? show error message .. wrong result? show the expected result and you actual result
      – scaisEdge
      Nov 7 at 9:07










    • No error message, the code works but it is delivering 0 results. As I said earlier the issue has only stopped working since I have been using encrypted data. It appears to be an issue with the decryption.
      – Phil Howell
      Nov 7 at 9:10












    • Remove the WHERE clause, and see if the values you selected under the aliases stfirst and stlast contain properly “decoded” names as you expect …?
      – misorude
      Nov 7 at 9:20










    • @PhilHowell .. answer updated .. (anyway seems strange that the where work using alias)
      – scaisEdge
      Nov 7 at 9:23
















    I had tried this and have just done so again but that isn't working either I'm afraid.
    – Phil Howell
    Nov 7 at 9:06




    I had tried this and have just done so again but that isn't working either I'm afraid.
    – Phil Howell
    Nov 7 at 9:06












    what mean "is not working" ?... you have error ? show error message .. wrong result? show the expected result and you actual result
    – scaisEdge
    Nov 7 at 9:07




    what mean "is not working" ?... you have error ? show error message .. wrong result? show the expected result and you actual result
    – scaisEdge
    Nov 7 at 9:07












    No error message, the code works but it is delivering 0 results. As I said earlier the issue has only stopped working since I have been using encrypted data. It appears to be an issue with the decryption.
    – Phil Howell
    Nov 7 at 9:10






    No error message, the code works but it is delivering 0 results. As I said earlier the issue has only stopped working since I have been using encrypted data. It appears to be an issue with the decryption.
    – Phil Howell
    Nov 7 at 9:10














    Remove the WHERE clause, and see if the values you selected under the aliases stfirst and stlast contain properly “decoded” names as you expect …?
    – misorude
    Nov 7 at 9:20




    Remove the WHERE clause, and see if the values you selected under the aliases stfirst and stlast contain properly “decoded” names as you expect …?
    – misorude
    Nov 7 at 9:20












    @PhilHowell .. answer updated .. (anyway seems strange that the where work using alias)
    – scaisEdge
    Nov 7 at 9:23




    @PhilHowell .. answer updated .. (anyway seems strange that the where work using alias)
    – scaisEdge
    Nov 7 at 9:23












    up vote
    0
    down vote













    Presumably, you are using MySQL. If so, MySQL extends SQL so you can use a HAVING clause with column aliases, So, you can write the query as:



    SELECT s.*, 
    AES_DECRYPT(firstname,UNHEX(SHA2('', 512))) AS stfirst,
    AES_DECRYPT(lastname,UNHEX(SHA2('', 512))) AS stlast
    FROM students s
    WHERE currentyear = $currentyear
    HAVING CONCAT(TRIM(stfirst), ' ', TRIM(stlast)) LIKE '%$result%'
    ORDER BY stlast;





    share|improve this answer

























      up vote
      0
      down vote













      Presumably, you are using MySQL. If so, MySQL extends SQL so you can use a HAVING clause with column aliases, So, you can write the query as:



      SELECT s.*, 
      AES_DECRYPT(firstname,UNHEX(SHA2('', 512))) AS stfirst,
      AES_DECRYPT(lastname,UNHEX(SHA2('', 512))) AS stlast
      FROM students s
      WHERE currentyear = $currentyear
      HAVING CONCAT(TRIM(stfirst), ' ', TRIM(stlast)) LIKE '%$result%'
      ORDER BY stlast;





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Presumably, you are using MySQL. If so, MySQL extends SQL so you can use a HAVING clause with column aliases, So, you can write the query as:



        SELECT s.*, 
        AES_DECRYPT(firstname,UNHEX(SHA2('', 512))) AS stfirst,
        AES_DECRYPT(lastname,UNHEX(SHA2('', 512))) AS stlast
        FROM students s
        WHERE currentyear = $currentyear
        HAVING CONCAT(TRIM(stfirst), ' ', TRIM(stlast)) LIKE '%$result%'
        ORDER BY stlast;





        share|improve this answer












        Presumably, you are using MySQL. If so, MySQL extends SQL so you can use a HAVING clause with column aliases, So, you can write the query as:



        SELECT s.*, 
        AES_DECRYPT(firstname,UNHEX(SHA2('', 512))) AS stfirst,
        AES_DECRYPT(lastname,UNHEX(SHA2('', 512))) AS stlast
        FROM students s
        WHERE currentyear = $currentyear
        HAVING CONCAT(TRIM(stfirst), ' ', TRIM(stlast)) LIKE '%$result%'
        ORDER BY stlast;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 7 at 12:27









        Gordon Linoff

        742k32285390




        742k32285390






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186239%2fconcat-trim-no-longer-working-after-encryption%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()