Jump to content

MySQL chars in string messing up query


rasmuspalm

Recommended Posts

Evening lovely php people.

Take a look at this code snippet:

$query = "SELECT AES_ENCRYPT('$plaintext','$key_string')";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$encrypted_string = $row[0];
}

$query2 = "SELECT AES_DECRYPT('$encrypted_string','$key_string')";
$result2 = mysql_query($query2) or die(mysql_error());

My problem is that ' and " in $plaintext, $key_string and especially $encrypted_string, will mess up my sql query. $encrypted_string can contain every possible character, and will frequently do so, since it's encrypted and all. Problem is i can't use something like htmlspecialchars() since that will alter the $encrypted_string and break the decryption process. I need a function something like: donotreadasmysql()  ;D
Any ideas?
Help will be rewarded with cookies :)

Link to comment
https://forums.phpfreaks.com/topic/32323-mysql-chars-in-string-messing-up-query/
Share on other sites

It's really pretty simple. Use the link I gave you and this:

http://dev.mysql.com/doc/refman/5.0/en/encryption-functions.html#function_aes_encrypt

if you can't figure it out from that I don't knwo what to tell you.

You escape it before you send it to be encrypted it, it encrypts it and puts it in the database, when you get it out of the database you then unescape it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.