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.
php sql database concat trim
|
show 1 more comment
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.
php sql database concat trim
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
|
show 1 more comment
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.
php sql database concat trim
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
php sql database concat trim
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
|
show 1 more comment
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
|
show 1 more comment
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
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 aliasesstfirst
andstlast
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
|
show 7 more comments
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;
add a comment |
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
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 aliasesstfirst
andstlast
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
|
show 7 more comments
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
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 aliasesstfirst
andstlast
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
|
show 7 more comments
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
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
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 aliasesstfirst
andstlast
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
|
show 7 more comments
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 aliasesstfirst
andstlast
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
|
show 7 more comments
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;
add a comment |
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;
add a comment |
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;
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;
answered Nov 7 at 12:27
Gordon Linoff
742k32285390
742k32285390
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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